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

I have problem with perl numeric expressions. Consider following code:
#!/usr/bin/perl use strict; use warnings; my $x1 = 44.7; my $x2 = 44.9-0.2; print "numbers $x1 $x2 - "; if ($x1 == $x2) { print "SAME\n"; } else { print "NOT SAME\n"; }

The result is:
numbers 44.7 44.7 - NOT SAME

This left me very much unsure. What's wrong? Perl or me?

Replies are listed 'Best First'.
Re: perl numeric expressions
by merlyn (Sage) on Nov 28, 2005 at 13:19 UTC
    This is a FAQ. It would be good for you to review at least the table-of-contents at "perldoc perlfaq" from time to time, even if you don't read the actual FAQs.

    The particular FAQ answers you'll need are:

    Why am I getting long decimals (eg, 19.9499999999999) instead of the + numbers I should be getting (eg, 19.95)? Internally, your computer represents floating-point numbers in bin +ary. Digital (as in powers of two) computers cannot store all numbers exactly. Some real numbers lose precision in the process. This is +a problem with how computers store numbers and affects all computer languages, not just Perl. perlnumber show the gory details of number representations and conversions. To limit the number of decimal places in your numbers, you can use + the printf or sprintf function. See the "Floating Point Arithmetic" fo +r more details. printf "%.2f", 10/3; my $number = sprintf "%.2f", 10/3; Why is int() broken? Your int() is most probably working just fine. It's the numbers th +at aren't quite what you think. First, see the above item "Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?". For example, this print int(0.6/0.2-2), "\n"; will in most computers print 0, not 1, because even such simple nu +mbers as 0.6 and 0.2 cannot be presented exactly by floating-point numbe +rs. What you think in the above as 'three' is really more like 2.9999999999999995559.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: perl numeric expressions
by monkey_boy (Priest) on Nov 28, 2005 at 13:23 UTC
    Use a fixed number of decimal places on your comparrison
    if (sprintf("%.1f", $x1) == sprintf("%.1f",$x2)) { print "SAME\n"; } else { print "NOT SAME\n"; }

    for more help see this tutorial



    This is not a Signature...
      Thanks!

      I, of course, knew, about FAQ mentioned by merlyn. I just thought it does not apply in this situation.

      Anyway thanks for example and tutorial.
Re: perl numeric expressions
by tphyahoo (Vicar) on Nov 28, 2005 at 13:50 UTC
    To my slight surprise, if you use Test::More:is() to compare the numbers, they are equal. I guess it depends, as Bill Clinton said, on what the definition of "is" is. In Test::More, it turns out, is is eq. Using the cmp_ok test with '==' fails, but the diagnostic is pretty unhelpful. I wonder, is there a way to get more explicit inequality diagnostics from tests?
    #!/usr/bin/perl use strict; use warnings; use Test::More qw(no_plan); my $x1 = 44.7; my $x2 = 44.9-0.2; is($x1,$x2, "44.7 is 44.9-0.2"); #succeeds. because is uses the eq ope +rator... cmp_ok($x1,'==',$x2, "44.7 == 44.9-0.2"); # fails. but the diagnostic +isn't real helpful.
    outputs:
    E:\data\learning>perl scratch.pl ok 1 - 44.7 is 44.9-0.2 not ok 2 - 44.7 == 44.9-0.2 # Failed test (scratch.pl at line 10) # got: 44.7 # expected: 44.7 1..2 # Looks like you failed 1 tests of 2.
Re: perl numeric expressions
by secret (Beadle) on Nov 28, 2005 at 13:23 UTC

    If you use eq instead of == it yields the correct result :

    #!/usr/bin/perl use strict; use warnings; my $x1 = 44.7; my $x2 = 44.9-0.2; print "numbers $x1 $x2 - "; if ($x1 eq $x2) { print "SAME\n"; } else { print "NOT SAME\n"; }

    .. but I'm not sure as why it is so !?

      it's because "eq" compares strings, not numbers.

        So it means that internally doing eq on a "number" is like doing the appropriate sprintf and == and that's how "extrapolation" of a number into a string works ?

Re: perl numeric expressions
by swampyankee (Parson) on Nov 28, 2005 at 15:24 UTC

    Floating point equality tests are, generally, conceptually, poor practice; I was warned about this when I took CS101 in 1971.

    Read, for some enlightment, David Goldberg, "What Every Computer Scientist Should Know about Floating Point" ACM Computing Surveys, 1991, Volume 23, Issue 1, pp 5-48.

    emc

    And remember:
    use strict; use warnings;