|Perl Monk, Perl Meditation|
Re^3: float values and operatorsby mpeg4codec (Pilgrim)
|on Aug 12, 2004 at 03:55 UTC||Need Help??|
Recipe 2.2 in the Perl Cookbook, Second Edition is excellent reading on this subject.
Basically, when you use a floating point number, such as 36.6, the computer doesn't store that exact value. Remember, computers are binary. When you get down to it, there's nothing more than transistors. The memory storage only really works well for integers. So the computer has to figure out some way to store approximations of the number.
To paraphrase the cookbook, the only numbers well-representable in binary are those involving powers of two. For example, .125 is 1/8. That's exactly representable in binary notation of floating point numbers.
However, take a number like 36.6. In fractional form, it would be 183/5, nothing to do with powers of two. The way the computer really stores the number can be seen:
(Code shamelessly copied from the Perl Cookbook.)
That gives us 36.600000000000001. Similarly, 36.8 is given as 36.799999999999997.
So now you see why the comparisons failed. You weren't working with exact values.
Perl's (and indeed, the C library's) rounding function are not exactly ``round up at five,'' but actually ``round towards even.'' So that way, sprintf will round 36.6 to an even 36.6 for calculating. So now, numerical comparisons work with nice and even numbers.
Take a look at the Perl Cookbook on this one. It's a very good read. I've just summarised what it has written very well.