[Tutor] Float number accuracy

Michiel Overtoom motoom at xs4all.nl
Thu Jul 10 22:54:14 CEST 2008


On 10-jul-2008, at 22:41, Julia wrote:


> >>> c = 3.3
> >>> c
> 3.2999999999999998
>
> I've done it with and without the c = float and still it rounds the  
> number down. Why? And more importantly: is it possible to make  
> Python more accurate? I need the exact number and not something  
> close to it for my new application.

That's because floats have only a fixed amount of bits to represent  
values, and not all values can be represented exactly, so there  
occurs some rounding errors.

Python can do exact math using the 'decimal' package. See
http://www.python.org/doc/2.4.3/lib/module-decimal.html

Greetings,


More information about the Tutor mailing list