# Incorrect results from fmod()?

• 04-11-2008
karlthetruth
Incorrect results from fmod()?
A coworker discovered what seemed to be a problem with the C# modulus operator for the float datatype. I wrote this little bit of C code and compiled it with MinGW and got the same results. I skimmed through several fmod related threads but found mostly programming problems. Since I got pretty much the same results in C# with VS2008 and C with gcc I figured someone else might have some insight.

Here is the code:

Code:

```#include <stdio.h> #include <math.h> int main(int argc, char **argv) {   double M1 = fmod(323.2,0.001);   double M2 = fmod(323200.0,1.0);   printf ("fmod(323.2,0.001)=");   printf ("%f", M1);   printf ("\n\n");   printf ("fmod(323200.0,1.0)=");   printf ("%f", M2);   printf ("\n"); }```
And here are the results:

fmod(323.2,0.001)=0.001000

fmod(323200.0,1.0)=0.000000

Note that the first one is the problem. It should output zero. Multiplying by 1000.0 gives the expected result. The problem was originally seen in C# and the result was slightly less than 0.001 (0.000999...).

Does that seem right to you?
• 04-11-2008
Dave_Sinkula
My guess is this.
• 04-11-2008
karlthetruth
Thanks! I will look that over. I assumed it was a binary-floating point problem, but I just don't get why it isn't compensated for in the function.
• 04-11-2008
Dave_Sinkula
Code:

```#include <stdio.h> #include <math.h> int main(int argc, char **argv) {   double a = 323.2, b = 0.001, M1 = fmod(a,b);   printf ("fmod(&#37;.15f,%.15f) = %.15f\n", a, b, M1);   return 0; } /* my output fmod(323.199999999999990,0.001000000000000) = 0.000999999999982 */```
• 04-11-2008
matsp
Quote:

Originally Posted by karlthetruth
Thanks! I will look that over. I assumed it was a binary-floating point problem, but I just don't get why it isn't compensated for in the function.

How would you compensate for that?

--
Mats