473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Strange behavior of fmod()

I have the following code snippet where I am validating time in minutes
and hours entered in as a double variable.

If I enter the value 10.59, the value printed out is 10:58. For this
input, the correct value is being passed to 'temp', so I just can't
figure out why fmod() is misbehaving.

Any ideas?
double InputTime, temp;
int Hours, Minutes;

cout << "Enter the start time: ";
cin >InputTime;

Hours = static_cast<int>(InputTime);
temp = InputTime - Hours;
temp = temp * 100.0;
Minutes = fmod(temp, 100);
cout << "Time is " << Hours << ":" << Minutes;
Aug 28 '06 #1
4 2143
Schizoid Man wrote:
If I enter the value 10.59, the value printed out is 10:58. For this
input, the correct value is being passed to 'temp', so I just can't
figure out why fmod() is misbehaving.
Taking a closer look, it seems that this code works properly till 10.50.

For 10.50 the time is 10:50, so far so good.
For 10.51 the time is 10:50, which is very odd indeed.

I really would appreciate some help.
Aug 28 '06 #2
Schizoid Man wrote:
Taking a closer look, it seems that this code works properly till 10.50.

For 10.50 the time is 10:50, so far so good.
For 10.51 the time is 10:50, which is very odd indeed.
On further analysis, it seems that this problem only occurs if the input
is between 10.51 and 10.71.

This is very strange indeed. I really would appreciate any help at all.

Thanks.
Aug 28 '06 #3

Schizoid Man wrote:
I have the following code snippet where I am validating time in minutes
and hours entered in as a double variable.

If I enter the value 10.59, the value printed out is 10:58. For this
input, the correct value is being passed to 'temp', so I just can't
figure out why fmod() is misbehaving.

Any ideas?
double InputTime, temp;
int Hours, Minutes;

cout << "Enter the start time: ";
cin >InputTime;

Hours = static_cast<int>(InputTime);
temp = InputTime - Hours;
temp = temp * 100.0;
Minutes = fmod(temp, 100);
cout << "Time is " << Hours << ":" << Minutes;
You need to look-up floating-point precision.

/Peter

Aug 28 '06 #4
double InputTime, temp;
int Hours, Minutes;

cout << "Enter the start time: ";
cin >InputTime;

Hours = static_cast<int>(InputTime);
temp = InputTime - Hours;
temp = temp * 100.0;
Floating point representation problem.
temp = 0.5899999999999;
Minutes = fmod(temp, 100);
now Minutes = 58, which is correct.

Try:
Minutes = fmod(temp+.1, 100);

to get rid of the problem.

Aug 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zunbeltz Izaola | last post by:
Hi, I have a problem with % operation. It returns a incorrect value in this case: >>> -2.77555756156e-17%1 1.0 where the returned value should be -2.77555756156e-17.
8
by: seia0106 | last post by:
Hello, I have a C++ program , which has following two lines of code z=atan2(x,y); z=(float)(fmod(2*pi+z, 2*pi); The comments written by the other programmer says that second line is used to...
2
by: Lonnie Princehouse | last post by:
I've been trying to debug this for two days now, and it's a longshot but I'm hoping that someone here might recognize a solution. I've got a C extension which calls a function in a C library,...
6
by: stau | last post by:
Hi! I'm reading a C book, and it says that fmod() returns the remainder of the exact division of it's arguments. Well, in a exact division, the remainder shall always be 0 (zero), so this don't...
2
by: Gintautas | last post by:
I'm trying to play a part of wav file. FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,0); plays all file FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,90000); plays file until 90000 sample...
17
by: joseph.p.doyle | last post by:
This code, compiled with visual studio .NET 2003, double a = 95.022, b = 0.01; printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a - floor(a / b) * b); a = 95.021, b = 0.01;...
14
by: Aaron Gray | last post by:
Does anyone have a good fmod() function written in Javascript ? Many thanks in advance, Aaron
7
by: bummerland | last post by:
Hi, I have a problem with the function fmod. Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0? tia bummerland
12
by: bsabiston | last post by:
Hi, I'm trying to get the fractional part of a floating point number. I've tried fmod() and modf(), and while both do work, they also occasionally return 1.0 for the fractional part of the number...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.