Incorrect results from fmod()?

This is a discussion on Incorrect results from fmod()? within the C Programming forums, part of the General Programming Boards category; A coworker discovered what seemed to be a problem with the C# modulus operator for the float datatype. I wrote ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    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?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    My guess is this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    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.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by karlthetruth View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 10:46 AM
  2. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2004, 12:46 AM
  3. error with fmod() function
    By minesweeper in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 06:24 AM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 03:39 PM
  5. It is not incorrect to use void main
    By momo20016 in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-22-2002, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21