Why not FP for Money?

Batista, Facundo FBatista at uniFON.com.ar
Wed Sep 22 12:02:30 EDT 2004


[Carlos Ribeiro]

#- Beware! Floating point errors may bite you *even for non-fractional
#- numbers*. Once the number of significant digits is bigger than the
#- precision of the mantissa, there will be automatic scaling of values,
#- dropping the least significant digit(s). Depending on the floating
#- point representation (single, double or extended, just to mention the
#- most common ones), this error will happen with numbers of different
#- magnitudes (from ~7 to ~19 significant digits, depending upon the
#- internal representation, if I remember it well). This error is
#- particularly nasty if you're repeatedly summing large quantities with
#- small ones.

And you can not change the precision of your processor's FPU. But you can
change it in Decimal:

>>> 1e10+1e-10
10000000000.0
>>> decimal.Decimal("1e10")+decimal.Decimal("1e-10")
Decimal("10000000000.0000000001")
>>> decimal.Decimal("1e20")+decimal.Decimal("1e-20")
Decimal("100000000000000000000.0000000")
>>> decimal.getcontext().prec=50
>>> decimal.Decimal("1e20")+decimal.Decimal("1e-20")
Decimal("100000000000000000000.00000000000000000001")

.	Facundo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040922/e1eb288a/attachment.html>


More information about the Python-list mailing list