[comp.lang.c] Precision in C

pcb@usl.usl.edu (Peter C. Bahrs) (11/14/88)

We are programming in Microsoft C v5.1 and MS Windows 2.03.

We are trying to convert a double precision number which represents
a delta time into a string which represents military time using the
"modf" function.

Example:
  double dTime = 101.1;

  double dt, dTemp, dMinutes, dHours, dSeconds;

                                               /* Below is the results */
dt = fmod (dTime, (double) 24.0);         /* dt now equals 5.1 */
dTemp = modf (dt, &dHours) * 60.0;        /* dtemp = 6.0, dHours = 5.0 */
dSeconds = modf (dTemp, &dMinutes) * 60.0; /* dSeconds = 60.0, dMinutes = 5.0 */
 sprintf (szBuff, "%02d:%02d:%02d", (int)dHours, (int)dMinutes, (int)dSeconds);

  Now szBuff is 05:05:59?  the correct answer should be 05:06:00

Question: Why is modf returning a 1.0 when calculating dSeconds?  The
docs say that the return value is the fractional portion.
 
Comment: We have printed every possible format of the double variables
to see if some residue was in the number (i.e. 6.000000000001) but
did not find anything.

I suspect some kind of representational problem in the compiler?

So.....!, the moral of the story is modf is returning a 1 !!!!!!! when 
it should return 0.

Thanks in Advance
pcb@usl.usl.edu   csnet