fmod(x, y) returns the remainder of x/y. However, as I found out, it doesn't return the exact remainder which is a pretty big problem.
For instance...
You can go ahead and compile this code. It shoudn't give you any warnings or crash your system.
CODE
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.55, y = 0.1, z;
z = fmod(x, y);
printf("z = %f\n", z);
/* z = 0.05 if six digits of precision is displayed */
/* however, z is actually greater than 0.05 which is a problem */
/* here's why... */
if(z == 0.05)
{
puts("Z is equal to 0.05");
}
else
{
puts("Z is not equal to 0.05");
}
/* In other words, the fmod() function does not return the perfect remainder. */
system("PAUSE");
return 0;
}
I need to be able to rely on fmod() returning the exact remainder. If anybody has an idea of a way around this problem, it would be greatly appreciated.