Inexplicable problem in a very simple piece of code



  • I'm learning about mod and how % doesnt work on floats.  That lead
    me to #include <cmath> which has the fmod function.  I wrote
    a short program to test the function but I do not see expected result.



    A program consisting of this line was used to show the remainder of 122.07/1.  I expect to see 0.07 as the answer.



    cout << "fmod((float) 122.07, (float) 1) = " << fmod((float) 122.07, (float) 1) << endl;



    Instead, this is the output:



    fmod((float) 122.07, (float) 1) = 0.0699997



    wtf?



  • This is the expected result. In floating point 0.0699997 == 0.07.

    How many digits of precision did you expect?



  • Im retarded.  I hope someone makes % work on floats when C+++ comes out. 



    It doesnt matter anymore, I found out about ostream and you can format floats to string that way instead.



  • @outer_space said:

    Im retarded.  I hope someone makes % work on floats when C+++ comes out. 



    It doesnt matter anymore, I found out about ostream and you can format floats to string that way instead.


    How about rounding the number down and subtracting it from the original?



  • I suggest you to use a fixed point notation, or to do an algoritmic's error analisys



  • Try using doubles instead of floats as they have more precision and wont be out by so much.

    E.g. :

    fmod((float) 122.07, (float) 1) = 0.0699997
    fmod((double) 122.07, (double) 1) = 0.07

    But as other people have said, knowning how/why floats/doubles have these limitations/effects is a very important thing and you should research it a bit.




  • BTW, testing to see if one floating point number is equal to another is always a risky proposition.  Floating point variable are part of a category called "approximate numeric".  Think about a physical analogy:  can you ever be certain that two pieces of wood are the same length.  Even if you cut them both the same length to the best of your ability, they are most likely a little bit different.

    The best aproach when comparing floats is to find the difference and see if it within your tolerance specifications.



  • I think you are better off using the [b]modf[/b] function.



  • This thread is your friend. You should read it.



  • If you really need precision you can use a bignum library like GMP (http://www.swox.com/gmp)



  • Ick... the % operator is for use with integer arithmatic for a reason.
    generally fmod isn't what you want to use, unless you have a good reason to. As in, you are working with a mathimatical function that requires it.  Otherwise you should stick to integers or built in formatting functions. Thats a WTF waiting to happen.



  • And with floating point comparisons why do frameworks (for me, .Net and Java) not remember to include a simple comparison function in their framework? Would save having to write it everytime you use a new language and it's a function that is needed anytime you want to compare floats, it's hardly a huge function. At least if it exists people browsing the framework documentation might find it by chance and educate themselves about this aspect of floats.


Log in to reply