monetary applications [was: Python GUI app to impress the bos s?]

Alex Martelli aleax at aleax.it
Fri Sep 20 03:08:53 EDT 2002


Paul Rubin wrote:
        ...
> I'm finding it hard to imagine how any real-world financial
> calculation using IEEE doubles could be off by as much as a penny.

I suggest:

http://www2.hursley.ibm.com/decimal/decifaq.html

and other material involving M. Cowlishaw's long campaign for
decimal arithmetic, including debates in the IEEE committee.
E.g.:

"""
A more typical example is applying a 5% sales tax on an item (such as a 
$0.70 telephone call), rounded to the nearest cent. 

Using double binary floating-point, the result of 0.70 x 1.05 is 
0.73499999999999998667732370449812151491641998291015625; the result should 
have been 0.735 (which would be rounded up to $0.74) but instead the 
rounded result would be $0.73.
"""

That's typical Python behavior today, btw (of course, since it uses
the underlying floating binary representation):

>>> 0.70 * 1.05
0.73499999999999999
>>>

Should your business get tax-audited, inspectors may take a dim view
of your shaving a cent off the sales tax due on each and every item
you sell to the public at $0.70 -- "I found it hard to imagine" how
rounding effects might affect the result (when the law specifies
rounding to the nearest cent and always rounding half cents up) may
not be an acceptable defense.


Alex




More information about the Python-list mailing list