Floating point bug?

Diez B. Roggisch deets at nospam.web.de
Thu Feb 14 03:17:01 EST 2008


Preston Landers schrieb:
> marek.rocki at wp.pl(marek.rocki at wp.pl)@2008.02.13 15:13:20 -0800:
>> Not a bug. All languages implementing floating point numbers have the
>> same issue. Some just decide to hide it from you. Please read
>> http://docs.python.org/tut/node16.html and particularly
>> http://docs.python.org/tut/node16.html#SECTION0016100000000000000000
>>
> 
> 
> This is true.  Fortunately Python does provide a module which allows
> you to work with exact floating point quantities.

That's a misconception. The decimal-module has a different base (10 
instead of 2), and higher precision. But that doesn't change the fact 
that it will expose the same rounding-errors as floats do - just for 
different numbers.

 >>> import decimal as d
 >>> d = d.Decimal
 >>> d("1") / d("3") * d("3")
Decimal("0.9999999999999999999999999999")
 >>>

The advantage is that the rounding errors are the ones expected in 
monetary caluclations, which means that you can write correct programs 
for such purposes.

Diez



More information about the Python-list mailing list