floating point woes

Mel mwilson at the-wire.com
Wed Feb 16 00:39:14 EST 2011


Hans-Peter Jansen wrote:

> Hi,
> 
> while I usually cope with the woes of floating point issues, this is
> one, that I didn't expect:
> 
>>>> round(2.385, 2)
> 2.3799999999999999
> 
> Doesn't the docs say, it's rounded up for this case?
> 
> <quote>
> 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
> </quote>
> 
> Well, that one is clearly rounding down.
> 
> What's up, eh, down here?

2.385 isn't really 2.385:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> repr (2.385)
'2.3849999999999998'
>>> 

so it correctly rounded down.  You need to use Decimal numbers if you want 
numbers that behave the way they look.

	Mel.




More information about the Python-list mailing list