C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Strange behavior of fmod()

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Schizoid Man
Guest





PostPosted: Mon Aug 28, 2006 9:10 am    Post subject: Strange behavior of fmod() Reply with 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;
Back to top
peter koch
Guest





PostPosted: Mon Aug 28, 2006 9:10 am    Post subject: Re: Strange behavior of fmod() Reply with quote



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





PostPosted: Mon Aug 28, 2006 9:10 am    Post subject: Re: Strange behavior of fmod() Reply with quote



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





PostPosted: Mon Aug 28, 2006 9:10 am    Post subject: Re: Strange behavior of fmod() Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.