Elapsed time program - incorrect results using "modf" function

For homework I need to make a program that reads in two times (in 24 hour time) and prints the time elapsed.

The way I'm working this out is as follows...

12 : 00 : 00 (First time)
13 : 30 : 05 (Second time)

12 * 60 * 60 = 43,200 (12 hours in seconds )

(13 * 60 * 60) + 30 * 60 + 25 = 48,605 (second time in seconds)

48,605 - 43,200 = 5405 (first time minus second time)

Now to get the time elapsed...

5405 / 60 / 60 = 1.501388889 (Store the integer part as hours elapsed)
0.501388889 * 60 = 30.08333334 (Store the integer part as minutes elapsed)
0.08333334 * 60 = 5.0000004 (Store integer part as seconds elapsed)

--------------------------------------------------------------------------------

I hope I made that clear, I put in bold the parts that represent the elapsed time. I've tested this out on lots of times so I'm pretty sure this calculation works.

Anyway This is the code I'm using for the calculation, and also where the problem is, it is so close to working, but for some reason it's always 1 second off! I have no idea why it's doing this, and I'm hoping someone here might be able to help.

void elapsedTime( double elapse )
{
double hours;
double minutes;
double seconds;

modf( (modf( ( modf( ( elapse / 60 / 60 ), &hours ) ) * 60, &minutes ) ) * 60, &seconds );

cout << "Hours: " << hours << "\tMinutes: " << minutes << "\tSeconds: " << seconds;
getch();

}


Here's (http://www.cplusplus.com/reference/clibrary/cmath/modf/) info on the modf function for those of you that don't know, it just splits the fractional part and the integer part of a number.

To be honest I'm not expecting someone to read though all this, let alone solve my problem, but if anyone does I'd be really thankful, and sorry if any of this is hard to read/unclear, I'm in a bit of a rush...
Sorry, I forgot to add that, for the times given in the example above, the function would print

"Hours: 1 Minutes: 30 Seconds: 4"

whereas it should be...

"Hours: 1 Minutes: 30 Seconds: 5"
This is the not quite exact nature of c++ floating point arithmetic.
You are expecting 5 but what you actually have is something like 4.99999999999.
You could add one more step - like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//*********************************************

void elapsedTime( double elapse )
{
double hours;
double minutes;
double seconds;
double extra_second;

modf (modf( (modf( ( modf( ( elapse / 60 / 60 ), &hours ) ) * 60, &minutes ) ) * 60, &seconds ) *2, &extra_second);
seconds += extra_second;

cout << "Hours: " << hours << "\tMinutes: " << minutes << "\tSeconds: " << seconds;

}


In other words - test the tenths of a second. Multiply it by 2 - so 0.5 and greater becomes 1.xxxxx and if it is less than 0.5 the result is 0.xxxxxx - so we end up adding 0 or 1 to the seconds - in other words we effectively round to the nearest second.
Oh I see...

Thanks for the reply, I'll consider you suggestion, but I think I might also have another fix...

instead of what I have in my original post, I have:

seconds = 60 * modf( ( modf( ( 4515.0 / 60 / 60 ), &hours ) ) * 60, &minutes );

This seems to be working for all the time variations I've tried, including those that didn't work previously.
i think if the time has to be 24HR format, we can use arithmetic on hours as it is...............
Topic archived. No new replies allowed.