Please help - get average

Batista, Facundo FBatista at uniFON.com.ar
Wed Oct 27 09:36:18 EDT 2004


[Robin Becker]

#- ...... interestingly the standard algorithms for 
#- means/deviations are 
#- numerically poor. As an example
#- 
#-  >>> (1.e30+1-1.0e30)/3
#- 0.0

It's not a problem of the algorithm, it's a problem of the precision of
binary floating point (which is fixed by your hardware).

If you want arbitrary precision, use Decimal:

>>> import decimal
>>> decimal.getcontext().prec=35
>>> d = decimal.Decimal("1e30")
>>> (d+1-d)/3
Decimal("0.33333333333333333333333333333333333")
>>> 

.	Facundo



More information about the Python-list mailing list