|Pathologically Eclectic Rubbish Lister|
eq vs ==by spikeheap (Novice)
|on Nov 10, 2009 at 11:40 UTC||Need Help??|
spikeheap has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to carry out a simple comparison of two numbers in Perl, but I have encountered some interesting results. If someone could point me to a foolish mistake somewhere I would be more than grateful!
I understand that eq carries out a string comparison, whereas == carries out a numerical comparison, however when I try to compare numbers after a simple arithmetic calculation == returns false when I would expect true, while eq returns true correctly.
prints523.2, 23.2, (500, 500), 1, .
I have come to the conclusion that this is because of floating point arithmetic (correct me if I'm wrong), as printing $num1 - $num2 yeilds a value of 1.13686837721616e-13.
My confusion comes because if I alter the above script from 23.2 to 0.2, the result for == is true (correctly), and I can't understand where the difference lies that should cause == to deviate in such a manner.
Of course the ultimate question is: is there something I'm doing (or not doing) which can account for this? Is there a better way of doing this simple comparison which will yield consistent results?