Why not FP for Money?

John Roth newsgroups at jhrothjr.com
Tue Sep 21 12:30:48 EDT 2004


"Chris Barker" <Barkmann at gmail.com> wrote in message 
news:cc887c1d.0409202325.76ec2227 at posting.google.com...
> Hi all,
>
> I promise this is not a troll... Really, it's embarassing, because
> this is one Engineer that DID take a Numerical Analysis course, and
> from William Kahan, no less, and I still don't really get it. (and
> yes, Alex, I could have gotten my degree without it)

It seems your course didn't make the one crucial distinction
that should be written in letters of fire at the start of every
discussion of floating point.

Floating point arithmetic is useful for continuous quantities,
or quantities that are measured (as in weighed, etc.) Integers
(and rationals) are useful for things that are counted, which
includes currency units, but also includes anything else that
comes in discrete units  (apples and oranges, for instance).

Rounding issues in floating point revolve around preserving
precision, and that can be a very intricate undertaking,
but it's reasonably well understood in the context of things
that are measured.

Rounding issues in fixed point have to do with the fact
that integer division yields rationals, and if you attempt
to approximate it with fixed point fractions, then you
need to do something to preserve precision.

As another poster already said, rounding issues in
currency have a great deal to do with custom and
government regulation, and have very little to do with
anything that makes intuitive sense (unless you're an
accountant, and sometimes not even then [grin].)

Trying to mix the two always gets people in trouble,
over and above trying to use binary fractions in a field
where everyone expects decimal fractions.

>
> I've seen it suggested various times that one should use fixed point
> for money, perhaps micro cents represented as integers. However, if
> you do that, you need to make sure you do all the rounding correctly.
> If you were to use FP, you could have your IEEE 754 hardware do the
> rounding for you, and get better performance to boot. If you are
> rounding to cents at the end anyway, I can't see that the errors you'd
> get with 64 bit FP numbers would ever be an issue.
>
> I understand that when your numbers got large enough, you'd start to
> lose the pennies, but how many of us have the luxury of doing
> computation with quadrillions of dollars actually care about the
> pennies? Besides, you could check for HUGE numbers and do something
> special with them if you wanted.
>
> What am I missing here?

Debits equal Credits to the penny, always have, and always will.
(at least since double entry bookkeeping was invented.) Any
imbalance will get the Great Gods of Accounting on your tail.

John Roth

>
> -Chris 




More information about the Python-list mailing list