Problem whit float precision, 1.6 Windows 98

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed Sep 13 08:43:49 EDT 2000


Juan Huertas wrote in comp.lang.python:
> Thank You, Remco.
> 
> But there are other problems whit the precision, for example:
> 
> >>> round(2020.685,2)
> 2020.6900000000001
> 
> Acording the Python Documentation, round(v,n):
> 
> Return the floating point value x rounded to "n digits after the decimal
> point"

Same problem - 2020.69 can't be represented either. What you get is the best
possible approximation. Moreover, repr() returns a string with the minimal
amount of digits so that eval(repr(x)) == x always holds.

But str(round(2020.685,2)) is "2020.69". The interactive interpreter uses
repr() to display the result of expressions.

Tim Peters is an expert on this, and he's posted a lot of things about
floats. For instance in
http://www.deja.com/=dnc/[ST_rn=ps]/getdoc.xp?AN=607818947 .

Or if you want overkill,
http://www.deja.com/=dnc/[ST_rn=ps]/getdoc.xp?AN=594131798 .

There is mention of a module that implements decimal floats, which would
behave like you'd expect,  but I don't know if that's for Python and I
don't know where to find it. Personally I would go for some fixed point
solution using integers, wrapped in a Money class or something.
-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  Murphy's Rules, "Heavy-Duty Rapid Fire":
   In Traveler (GDW) a spaceship's weapons can fire only once every
   sixteen minutes. Most battles last several hours.



More information about the Python-list mailing list