problem in fmod()

This is a discussion on problem in fmod() within the C++ Programming forums, part of the General Programming Boards category; Code: cout << fmod(6.0*0.02, 0.02) << endl; cout << fmod(6*0.02, 0.02) << endl; cout << fmod(5.0*0.02,0.02) << endl; cout << ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    problem in fmod()

    Code:
            cout << fmod(6.0*0.02, 0.02) << endl;
    	cout << fmod(6*0.02, 0.02) << endl;
    	cout << fmod(5.0*0.02,0.02) << endl;
    	cout << fmod(5*0.02,0.02) << endl;
    	cout << fmod(3.0*0.02,0.02) << endl;
    	cout << fmod(3*0.02,0.02) << endl;
    
    (output)
    0.02
    0.02
    3.46945e-018
    3.46945e-018
    0.02
    0.02
    The answers are supposed to be all zero.
    I'm not sure why fmod() shows incorrect results for every multiple of 3 (3, 6, 9, ...)?
    Does anyone have any idea?
    Thanks.
    Last edited by Salem; 08-08-2010 at 10:27 PM. Reason: Added [code][/code] tags - learn about them

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    fmod() is working correctly. What you're seeing has to do with binary floating-point being an inexact representation of decimal values.

    Let's look at the first parameters to your fmod() calls (there are only three unique ones):

    1) 6*0.02 : in decimal this is 0.12; in double-precision binary floating-point it's 0.119999999999999995559107901499373838305473327636 71875

    2) 5*0.02 : in decimal this is 0.1; in double-precision binary floating-point it's 0.100000000000000005551115123125782702118158340454 1015625

    3) 3*0.02 : in decimal this is 0.06; in double-precision binary floating-point it's 0.059999999999999997779553950749686919152736663818 35937

    Now let's look at the second parameter, 0.02. In double-precision binary floating-point it's 0.020000000000000000416333634234433702658861875534 0576171875

    (Sorry about the spaces inserted in my long decimal values -- that is formatting done by cboard that I can't control.)

    If you perform fmod() on these actual values, your results make sense (although cout won't print them to the detail I've shown above -- see my article Print Precision of Dyadic Fractions Varies by Language - Exploring Binary).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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