qbxk has asked for the wisdom of the Perl Monks concerning the following question:

It's late here, I'm tired, and I was questioning my sanity until I distilled my bug to it's essence. Now I'm questioning perl's sanity. What's going on here?
my $x = 1.1 - 1.0; print(qq[$x <= 0.1 ? ] . ($x <= 0.1 ? q[yes] : q[no] ));
I won't tell you what gets printed, because I don't believe it. Try it on your command line. What do you get? Why?????? Then, should this be different? It is.
my $x = 1.0 - 0.9; print(qq[$x <= 0.1 ? ] . ($x <= 0.1 ? q[yes] : q[no] ));
fwiw,
~$: perl -v This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
using my $x = 1.2 - 1.1 is even more interesting.....

i look forward to your wise clarifications in the morning, and easing my mind greatly, this monk must now get some rest....


It's not what you look like, when you're doin' what you’re doin'.
It's what you’re doin' when you’re doin' what you look like you’re doin'!
     - Charles Wright & the Watts 103rd Street Rhythm Band, Express yourself

Replies are listed 'Best First'.
Re: bug in floating point math?
by ikegami (Patriarch) on Sep 23, 2010 at 03:40 UTC

    In a sense, yes, it is a bug. The "known limitation" kind of bug. 1/10 is a periodic number in binary like 1/3 is in decimal. To store it precisely as a float would take infinite storage.

    1.1 and 0.1 cut off at a different point, so 1.1-1.0 ends up being slightly different than 0.1.

    $ perl -e'printf "%.20e\n", 0.1' 1.00000000000000005551e-01 $ perl -e'printf "%.20e\n", 1.1-1.0' 1.00000000000000088818e-01
Re: bug in floating point math?
by BrowserUk (Patriarch) on Sep 23, 2010 at 09:04 UTC