Because of the way that most machines handle floating-points, they're frequently not what they say they are. From PerlFaq 4:
Internally, your computer represents floating-point numbers in binary. Floating-point numbers read in from a file or appearing as literals in your program are converted from their decimal floating-point representation (eg, 19.95) to the internal binary representation.If you really, really need exact handling of floating-point numbers, you can (at a good deal of computational expense) use Math::BigFloat. Numbers which use Math::BigFloat are internally handled as strings, so you won't get the strange rounding errors you're experiencing. Plus, it provides a bround method that does the exact rounding you want.However, 19.95 can't be precisely represented as a binary floating-point number, just like 1/3 can't be exactly represented as a decimal floating-point number. The computer's binary representation of 19.95, therefore, isn't exactly 19.95.
stephen
In reply to Re: strange rounding behaviour
by stephen
in thread strange rounding behaviour
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |