Q&A > math
Description:
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 FloatingPoint 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).

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.

Answer: My floating point comparison does not work. Why ? contributed by hanspr
Then file x2.txt And then run this code The result is So the only reliable solution I have found till now is Result

Please (register and) log in if you wish to add an answer
 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
For:  Use:  
&  &  
<  <  
>  >  
[  [  
]  ] 