 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Schizoid Man Guest
|
Posted: Mon Aug 28, 2006 9:10 am Post subject: 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; |
|
| Back to top |
|
 |
peter koch Guest
|
Posted: Mon Aug 28, 2006 9:10 am Post subject: Re: Strange behavior of fmod() |
|
|
Schizoid Man wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Schizoid Man Guest
|
Posted: Mon Aug 28, 2006 9:10 am Post subject: Re: Strange behavior of fmod() |
|
|
Schizoid Man wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Schizoid Man Guest
|
Posted: Mon Aug 28, 2006 9:10 am Post subject: Re: Strange behavior of fmod() |
|
|
Schizoid Man wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|