Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

My floating point comparison does not work. Why ?

by thens (Scribe)
on Sep 23, 2003 at 05:29 UTC ( #293402=categorized question: print w/replies, xml ) Need Help??
Contributed by thens on Sep 23, 2003 at 05:29 UTC
Q&A  > 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.


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)

Answer: My floating point comparison does not work. Why ?
contributed by PhilHibbs

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.

Answer: My floating point comparison does not work. Why ?
contributed by Ovid

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; }
Answer: My floating point comparison does not work. Why ?
contributed by TedPride

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.

Answer: My floating point comparison does not work. Why ?
contributed by dbwiz

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 !!
Answer: My floating point comparison does not work. Why ?
contributed by hanspr

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
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); }
t = 7566.85 st = 7566.85 Equal

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2021-03-02 18:14 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (57 votes). Check out past polls.