Floating point exactness & alternatives (summary)

Henry Spencer henry at zoo.toronto.edu
Wed Aug 8 03:30:30 AEST 1990


In article <713 at tetrauk.UUCP> rick at tetrauk.UUCP (Rick Jones) writes:
>... This is nothing more taxing (!) than business systems, which are not
>known for requiring complex mathematics.  They don't, but they do require
>exactness.  Auditors have this annoying view that accounts must balance to the
>penny, not 1 part in 10 to-the-something.  There is a good case for using some
>form of BCD representation, but there are many programming advantages in using
>the embedded numerical types of the language...

Our own experience, in building accounting systems for our own use, is that
the problem can often be eliminated by ignoring the decimal point in dollar
currencies and thinking of money as measured in pennies.  Then integer
arithmetic suffices, if amounts are not huge.  If amounts *are* huge, note
that most floating-point boxes will do precise integer arithmetic if you
are careful to avoid operations that necessarily produce fractions.

Yes, you get fractional amounts if you (say) buy 3.5 tons of stuff that
is priced at $1.17 per ton... but there really *is* a fractional amount
there, and the proper response is to explicitly round it in whatever
way is appropriate to the problem (in this case, the standard business
approach of rounding up).

This isn't necessarily an adequate solution to tricky cases involving things
like multiple currencies, but for simple stuff, it's amazing how much
simpler the problem gets if you don't insist on representing money as a
fraction just because it's written that way.
-- 
The 486 is to a modern CPU as a Jules  | Henry Spencer at U of Toronto Zoology
Verne reprint is to a modern SF novel. |  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list