Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Results depending on evaluation order

by fishy (Scribe)
on Apr 23, 2014 at 16:30 UTC ( #1083387=perlquestion: print w/ replies, xml ) Need Help??
fishy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I was trying the code from Okay! What!?!?!?
my $value1 = 14.4; print "value1 = $value1\n"; my $value2 = 10 + 14.4 - 10; print "value2 = $value2\n"; if ( $value1 == $value2 ) { print "value 1 equals value 2\n"; } else { print "value 1 does not equal value2\n"; }
Output:
value1 = 14.4 value2 = 14.4 value 1 does not equal value2

and from My floating point comparison does not work. Why ?
$number = 1.80; $premium = $number * ( 1 + 10/100 ); $expected = 1.98; print "Number 1 : $premium \n"; print "Number 2 : $expected \n"; print "Not" if $expected != $premium; print "Equal !! ";
Output:
Number 1 : 1.98 Number 2 : 1.98 NotEqual !!

when I came up with the following behavior (tested on Perl 5.10.1 and on 5.18.2):

Swapping operands on the first sample code:
my $value2 = 10 + 14.4 - 10; # wrong evaluated
my $value2 = 14.4 + 10 - 10; # wrong evaluated
my $value2 = 10 - 10 + 14.4; # right evaluated

Using eval:
my $value2 = eval (10 + 14.4 - 10); # right evaluated


Using eval on the second sample code:
$number = 1.80; $premium = eval ($number * ( 1 + 10/100 )); $expected = 1.98; print "Number 1 : $premium \n"; print "Number 2 : $expected \n"; print "Not" if $expected != $premium; print "Equal !! ";
Output:
Number 1 : 1.98 Number 2 : 1.98 Equal !!


So, I got odd results depending on the order of the operands and right results on evaluating.

How can that be?

Replies are listed 'Best First'.
Re: Results depending on evaluation order
by LanX (Chancellor) on Apr 23, 2014 at 16:38 UTC
    left to right evaluation:

     10 - 10 + 14.4 = (10 - 10) + 14.4 = 0 + 14.4 = 14.4

    I already explained that the mantissa needs to be shifted to loose precision.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Interesting. Thanks Rolf!

      What about eval? Why it got right evaluated?

        eval expects a string, so the number is converted to a string, and the stringification rounds a tiny bit, so it accidentally produces the right result.

Re: Results depending on evaluation order
by InfiniteSilence (Curate) on Apr 23, 2014 at 18:47 UTC

    There is a difference between what is represented and what is actually being stored,

    DB<11> p sprintf("%.8f", $expected); 1.98000000 DB<10> p sprintf("%.8f", $premium); 1.98000000 DB<12> p ($expected == $premium) DB<13>
    When you subtract the two the difference is exposed:
    DB<16> p ($expected - $premium) -2.22044604925031e-16

    But it is very small. Let's lop off the unnecessary information:

    DB<13> $pa = 1.98 DB<14> $pb = 1.98 DB<15> p ($pb == $pb) 1 DB<17> p ($pb - $pa) 0 DB<18> DB<22> $exp = sprintf("%.8f", $expected) DB<23> $pre = sprintf("%.8f", $premium) DB<24> p ($pre - $exp) 0

    So you see if you set limits on what Perl stores you should get more accurate comparisons.

    Celebrate Intellectual Diversity

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1083387]
Approved by rnewsham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2016-08-28 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (396 votes). Check out past polls.