http://qs321.pair.com?node_id=293402

thens has asked for the wisdom of the Perl Monks concerning the following question: (math)

You want to compare two floating point numbers and perl complains that they are not equal even though they are equal.

Sample code:

$number = 1.80; $premium = $number * ( 1 + 10/100 ); # 1.8 + 10% of 1.8 $expected = 1.98; # As we know 1.8 + 10% of 1.8 is 1.98 print "Number 1 : $premium \n"; print "Number 2 : $expected \n"; print "Not" if $expected != $premium; print "Equal !! ";

The output is

Number 1 : 1.98 Number 2 : 1.98 NotEqual !!

Well, by now you should be thinking perl is crazy. Let me explain what happens here.

The floating point numbers are stored in binary format in the computer and even though 10/100 = 0.1 is a finite decimal in base 10 arithmetic, when converted to binary floating point it has to be rounded off at some point. Hence when it is converted back to decimal we will get 0.999999 or .1000001 and not 0.1 as we would expect. So comparing floating point numbers for equality wont give the correct results.

But when I printed the numbers it was showing properly!

This is because while printing the numbers they are rounded off and hence we saw the same numbers even though their internal representation varied by a small fraction.

Solution:

While comparing floating point numbers we will have to test for range and not for equality.

=pod Sub : isEqualFloat Desc : to compare two floating point numbers and find out if they are +equal Args : float1, float2, delta value(optinal) or 0.00001 Returns : True if they are apart by the delta value, false otherwise =cut sub isEqualFloat($$$) { my( $float1, $float2, $delta ) = @_; $delta ||= 0.00001; # default value of delta abs( $float1 - $float2 ) < $delta } # call as if ( isEqualFloat( 1.98, ( 1.8 * (1 + 10/100) )) ) { ... } # for high precision comparison if ( isEqualFloat( 1.98, ( 1.8 * (1 + 10/100) ), 0.0000001 ) ) { ... }

For further details, see What Every Computer Scientist Should Know About Floating-Point Arithmetic (PDF, 240kb)

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: My floating point comparison does not work. Why ?
by PhilHibbs (Hermit) on Sep 23, 2003 at 14:16 UTC
    The problem with comparing two floats with a tolerance is that the comparison is not transitive. That is, even though a==b and b==c, it may be that a!=c.

    This might become important in such situations as passing a sorting or searching function. I am more familiar with this situation in C++ and Java. In Java, there is such a thing as the "equals contract", which states that the equals function must be reflexive (a==a), symmetric (a==b, b==a) and transitive (a==b, b==c, a==c). This is important for building HashSet and HashMap objects, and there are similar rules in C++ for the STL containers and algorithms.

    Being something of a perl noob, I don't know if there are similar constraints in Perl libraries, but it's a good rule to bear in mind anyway.

    If a comparison function is not transitive, you are just pushing the surprising behaviour into a darker corner.

Re: My floating point comparison does not work. Why ?
by Ovid (Cardinal) on Sep 23, 2003 at 13:41 UTC

    Computers generally don't handle floating point numbers the way we expect them to. For instance, while you might think a number is 4.3, the computer might store it as 4.2999999999999. For most applications, this is probably fine, but you have to round the results when you display them. To compare two floating point numbers, use the sprintf function, figure out how many significant digits you need and compare with a string equal comparison operator (eq).

    sub floats_eq { my ($num1, $num2, $sig) = @_; $_ = sprintf "%.${sig}g" foreach $num1, $num2; return $num1 eq $num2; }

      But I guess the round off operation is very costly and may not be a good solution when you are dealing with large sets of data to be compared.

      When perfomance is a concern testing for the range is a better option than rounding off the numbers and then doing a string compare ( which is again costly !! )

      -T

Re: My floating point comparison does not work. Why ?
by TedPride (Priest) on Oct 30, 2004 at 23:22 UTC
    You should also note that while floating point numbers won't be exact, two numbers arrived at through the same calculation (4 / 7, for instance) will be the same. This means that under some conditions, rounding isn't necessary - and under others, a simple fraction class is probably going to be better than trying to approximate comparisons.
Re: My floating point comparison does not work. Why ?
by dbwiz (Curate) on Sep 23, 2003 at 12:49 UTC

    Your reasoning is sound. I have only a small remark. If you want to print the two numbers to show their differences, increase the number of decimals.

    #!/usr/bin/perl -w use strict; my ( $number, $premium, $expected ); $number = 1.80; $premium = $number * ( 1 + 10/100 ); # 1.8 + 10% of 1.8 $expected = 1.98; # As we know 1.8 + 10% of 1.8 is 1.98 printf "Number 1 : %20.19f\n", $premium ; printf "Number 2 : %20.19f\n", $expected ; print "Not" if ( $expected != $premium ); print "Equal !! "; __END__ Number 1 : 1.9800000000000002043 Number 2 : 1.9799999999999999822 NotEqual !!
Re: My floating point comparison does not work. Why ?
by hanspr (Sexton) on Mar 31, 2012 at 20:24 UTC

    If you create a text file x.txt with following data
    227.20 1176.00 262.00 481.60 492.00 489.60 808.50 934.50 150.00 806.00 563.70 295.60 552.90 327.25 0.00 0.00 0.00
    Then file x2.txt
    7566.85
    And then run this code
    open (X,"< x.txt"); while ($n = <X>) { $n =~ s/\n|\r//g; # same with or without $t += $n; } close X; $st = `cat x2.txt`; $st =~ s/\n|\r//g; # same with or without print "t = $t\n"; print "st = $st\n"; if (equal($t,$st,5)) { print "Equal\n"; } else { print "Different\n"; } sub equal { my ($A,$B,$dp) = @_; return sprintf("%.${dp}g",$A) eq sprintf("%.${dp}g",$B); }
    The result is
    t = 7566.85 st = 7566.85 Different
    So the only reliable solution I have found till now is
    sub equal { my ($A,$B,$dp) = @_; if ($A == $B) { return 1; } elsif ($A eq $B) { return 1; } return sprintf("%.${dp}g",$A) eq sprintf("%.${dp}g",$B); }
    Result
    t = 7566.85 st = 7566.85 Equal
A reply falls below the community's threshold of quality. You may see it by logging in.