C - floor of double moduls integer
Question
I need to do the following equation floor(e%100000) where e is a double. I know mod only accepts int values, how do I go about achieving this same result?
Thanks
Solution
Use the fmod()
function instead of %
. It accepts double
parameters, and returns a double
result.
OTHER TIPS
Why don't you take the floor first, then mod, ie. floor(e) % 100000
?
Perhaps I've misunderstood what you're trying to achieve. Could you give an example of the input and output you expect?
use fmod
You could use division to make the equivalent of modulo:
double e = 1289401004400.589201;
const double divisor = 100000.0;
double remainder = e - floor(e / divisor) * divisor;
double result = floor(remainder);
printf("%f\n", result);
This prints
4400.000000
Of course, this is much slower than any built-in modulo...
You could also just use fmod
, as Anders K. suggested :)
Edit
Fixed std::cout (C++) reference to use printf (C). Fixed change to output. Now it is purely C.