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

Try this sample code

#!/usr/local/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 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. <b Solution

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

;# 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 otherwis +e ;#------------------------------------------------------------ sub isEqualFloat{ my ( $float1, $float2, $delta ) = @_; $delta ||= 0.00001; # default value of delta return ( abs ( $float1 - $float2 ) < $delta ) }

call this function as

if ( isEqualFloat(1.98, ( 1.8 * (1 + 10/100) )) ) { # do something ... } # for high precision comparison if ( isEqualFloat(1.98, ( 1.8 * (1 + 10/100) ), 0.0000001) ) { # do something ... }

References

  • What Every Scientist Should Know About Floating-Point Arithmetic for more details on Floating point arithmetic and the internal representation.

    -T

    Originally posted as a Categorized Question.


    In reply to My floating point comparison does not work. Why ? by thens

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.