finding out the precision of floats

Bart Ogryczak B.Ogryczak at gmail.com
Tue Feb 27 09:09:40 EST 2007


On Feb 27, 1:36 pm, Facundo Batista <facu... at taniquetil.com.ar> wrote:
> Arnaud Delobelle wrote:
> > (and I don't want the standard Decimal class :)
>
> Why?

Why should you? It only gives you 28 significant digits, while 64-bit
float (as in 32-bit version of Python) gives you 53 significant
digits. Also note, that on x86 FPU uses 80-bit registers. An then
Decimal executes over 1500 times slower.

>>> from timeit import Timer
>>> t1 = Timer('(1.0/3.0)*3.0 - 1.0')
>>> t2 = Timer('(Decimal(1)/Decimal(3))*Decimal(3)-Decimal(1)',
'from decimal import Decimal')
>>> t2.timeit()/t1.timeit()
1621.7838879255889

If that's not enough to forget about Decimal, take a look at this:

>>> (Decimal(1)/Decimal(3))*Decimal(3) == Decimal(1)
False
>>> ((1.0/3.0)*3.0) == 1.0
True








More information about the Python-list mailing list