-
Dividing a time variable
So i have this -
time_t t4 = time(NULL);
//and then some stuff
time_t t5 = time(NULL);
iElapsedYah = t5-t4;
fElapsedYah2 = iElapsedYah/iNumTimes;
printf("It took %f seconds", fElapsedYah2);
but it always prints 0.0000000, and i dont know why even though individually iElapsedYah = not zero and iNumTimes = not zero
i need help!!!!!!!!!!!
i am using windows 7 and m complier is Dev C++
-
Integer division will never produce anything more precise than zero. Try casting either iElapsedYah or iNumTimes to double, you'll probably get better results.
-
Dividing two integral values truncates towards zero (except with some older compilers, when a negative operand is involved).
Converting the value to floating point AFTER the truncation has occurred does not change that. So, if the elapsed time (between the two time() calls) is small enough, the result of division will be zero.
Try "fElapsedYah2 = (double)iElapsedYah/iNumTimes;" I'm assuming fElapsedYah2 is of type double.
-
yup ^ works, thanks prelude and grumpy