Roedy Green said:
If you use doubles, you have about 14 significant digits. Let's say
your are working on a $100,000.00 mortgage. That gives you 7 places
for fractions of a penny. Your loop will go no more than 50 years. So
I think it should be safe for a novice to close his eyes and ignore
roundoff errors.
Depending on your customer, you'd better not do that. If you have different
programs (say legacy, when you are talking about 50 years) that slightly round
different at certain points, maybe used a different number of significant
digits, etc., you will find a whole bank looking for the missing penny.
An insurance company insider told me that they once spotted a penny difference
in calculations and a good part of the company went looking for it. It later
turned out that some ingenious technician (programmer?) booked fractions of
pennies of many transactions onto his account or the pennies, when there was a
full penny difference. Since a lot of accounts were involved, there was a
steady money flow (at the time of the tale the fraud suit was pending). The
insider also said that they wouldn't have made such a fuss, if this had
involved a million or so (probably tongue in cheek). [If you are an urban
legend/FOAF hunter: I have long lost contact with the man, so I can't prove
this anymore. It was in the late 80's.]
If, on the other hand, you are writing a program estimating your grandson's
dividend for certain saving plans, 14 digits should suffice.
When I worked for Univac back in the 1970s, I was dismayed to discover
that every financial institution in the USA computes interest a
different way, and that way is generally considered a secret. I
worked on a banking package called First/90 that allowed banks to plug
in their own interest-computing module, with no default.
I would hope by now consumer advocates would have managed to enforce a
uniform algorithm for both savings accounts and mortgages.
I don't know. Consider the legacy problem above. Banks and insurance companies
are very conservative.
Michael