This math scares me

Terry Reedy tjreedy at udel.edu
Thu Mar 15 12:52:55 EST 2001


Me:
> >> >If one counts pennies instead of dollars, then 32 bit ints (+-
> >> >$20 million) will serve for many purposes.  Exact, faster, and
> >> >more widely available than BCD
Grant Edwards:
> >> In fiancial calculations you also need to represent numbers
> >> that aren't amounts of money.  Interest rates for example.
Me:
> >So?
Grant
> So 32 bit integers with a weighting of one penny per count
> won't work for that.
Me:
> >Floats (double precision) work well enough for this, I believe.
Grant:
> No.  The binary floating point representation of many decimal
> fraction values is not exact.  That's what started this whole
> thread.  You can't represent 0.1 exactly in binary FP.  You can
> in BCD.

So, .1% gets represented as the binary equivalent of, perhaps,
.0010000000000000001.
So the interest on 2,000,000,000 pennies is calculated as
2000000.0000000002 instead of
2000000 pennies.  When rounded off, they are the same.  To me, this is, as
I claimed. 'well enough' (for many purposes, at least), even though not
'exact'.  Even if you are using 64 bit ints to allow larger amounts,  the
rounded error doesn't reach a whole penny until your dollar amounts are
about 50 quadrillion (US version) dollars (maybe give or take a power of
ten).  Haha.  The US Feds with all their BCD_equipped IBM mainframes can't
keep track of spending to within a billion dollars.

Now suppose a financial instrument carries a yearly interest rate of .077
(7.7%) with montly payments.  Then the monthly multiplier is
.0006416666666666666666666666666666666666...
Whoops, the BCD representation has to be approximate also, just like the
binary one.

Even if a monthly multiplier like .00643 (corresponding to a yearly 7.716%)
is exactly represented in BCD, there may still be fractional amounts that
need to be rounded to the nearest penny by one rule or another.  BCD does
not eliminate the problems of finite precision arithmetic.

Terry J. Reedy





More information about the Python-list mailing list