Decimal arithmetic, was Re: Python GUI app to impress the boss?
Christopher Browne
cbbrowne at acm.org
Sat Sep 21 05:39:48 CEST 2002
Paul Rubin <phr-n2002b at NOSPAMnightsong.com> wrote:
> Christopher Browne <cbbrowne at acm.org> writes:
>> The COBOL (or PL/I, or anyone else accustomed to BCD data types) guy
>> will justifiably look at the bizarro FP rounding as being, well,
>> bizarre.
>>
>> There's _no_ good reason for subtracting two perfectly good decimal
>> values from one another to result in having weird artifacts appear in
>> some 17th decimal place, aside from having a willingness to admit
>> fairly deranged arithmetic.
>
> Um, put $1000.00 in an imagined bank account and have it collect 5%
> annual interest, compounded daily for 25 years. How much money is in
> the account at the end?
>
> The "true" answer is (1+(.05/365))**(365*25) dollars, assuming 365 day
> years.
>
> The IEEE double precision answer will be extremely close to that
> amount.
>
> What happens if you do that calculation in BCD? How would you even
> specify how to do it? I don't think you will get nearly as much
> accuracy.
REALITY CHECK for a moment.
What do you think A Real Live bank uses to compute interest?
Financial transactions are most certainly /not/ computed based on the
quasi-infinite precision that you are imagining.
Were you expecting IEEE double precision arithmetic, applied over the
period of 25 years?
Or perhaps, instead, might they use BCD arithmetic, applied on each
date that interest is to be accrued?
If I put $1000 in a /real/ bank account, earning 5% interest,
compounded daily, paid monthly, it is more than just /slightly/ likely
that the bank in question would, using code likely written in PL/I or
COBOL, put money into the account every month, with amounts rounded
based on BCD arithmetic.
The "true answer" will most certainly be the BCD-based answer; that's
the amount of money you'd have in the account 25 years later.
If you protest that the "true" answer is supposed to be
(1+(.05/365))**(365*25) dollars, and that the bank is short-changing
you by 7 cents, the bank will quite glibly tell you to look at the
interest policy, which /won't/ be based on waiting 25 years to apply
the interest.
--
(reverse (concatenate 'string "gro.mca@" "enworbbc"))
http://cbbrowne.com/info/oses.html
One man's constant is another man's variable. -- Alan Perlis
More information about the Python-list
mailing list