Why not FP for Money?

Michael Hobbs michael at hobbshouse.org
Tue Sep 21 19:32:14 EDT 2004


> What am I missing here?

At the most fundamental level, the problem is that 2 or any power of 2
cannot be evenly divided by 10. Let me explain...

Every currency system that I know of uses decimal fractions to represent
cents, shillings, etc. Therefore, to accurately represent precise currency
values, one needs to either use the decimal system, or a system that can
precisely map to the decimal system. Binary, unfortunately, is *not* such
a system.

To represent one half in decimal, you would write 0.5 because 5 * 2 = 10;
carry the decimal = 1.0. To represent one half in binary, you would write
0.1 because 1 * 10 = 10; carry the decimal = 1.0. Following the same
exercise, to represent one fourth in decimal you would write 0.25 because
25 * 4 = 100; carry the decimal = 1.0. To represent one fourth in binary
you would write 0.01 because 01 * 100 = 100; carry the decimal = 1.0. The
problem comes when trying to represent one tenth. Again, to represent one
tenth in decimal you would write 0.1 because 1 * 10 = 10; carry the
decimal = 1.0. To represent one tenth in binary, however, you would write
0.110011001100110011001100 (and so on) because two is not evenly divisible
by ten. Therefore, there is no number that can be multiplied by 1010 (ten)
to yield 1, 10 (two), 100 (four), 1000 (eight), or any other multiple of
two.

We often encounter this same problem when trying to represent thirds in
the decimal system. Since 10 is not evenly divisible by 3, we have to
approximate one third by writing 0.33333333333333 (and so on).

The end result is that you cannot use binary fractions to precisely
represent decimal fractions. To represent decimal fractions in a binary
system, you first need to shift the decimal point so that there is no more
fraction and then work from there.






More information about the Python-list mailing list