in reply to strange rounding behaviour
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: strange rounding behaviour
by riffraff (Pilgrim) on May 18, 2001 at 20:35 UTC | |
by srawls (Friar) on May 19, 2001 at 00:03 UTC | |
by riffraff (Pilgrim) on May 19, 2001 at 00:26 UTC |