floating point woes
clp2 at rebertia.com
Wed Feb 16 01:09:58 CET 2011
On Tue, Feb 15, 2011 at 3:49 PM, Hans-Peter Jansen <hpj at urpla.net> wrote:
> while I usually cope with the woes of floating point issues, this is
> one, that I didn't expect:
>>>> round(2.385, 2)
> Doesn't the docs say, it's rounded up for this case?
> Values are rounded to the closest multiple of 10 to the power minus n;
> if two multiples are equally close, rounding is done away from 0
> Well, that one is clearly rounding down.
> What's up, eh, down here?
Note: The behavior of round() for floats can be surprising: for
example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This
is not a bug: it’s a result of the fact that most decimal fractions
can’t be represented exactly as a float. See "Floating Point
Arithmetic: Issues and Limitations" for more information.
>>> from decimal import Decimal
Which, rounded to 2 decimal places, gives us 2.38, which is in turn
I encourage reading .
More information about the Python-list