Why not FP for Money?
John Burton
john.burton at jbmail.com
Wed Sep 22 03:13:53 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)
>
> 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.
On my version of python:
# I spent £1 and 13 pence
p = 1.13
# How many pence is that?
print int(p*100)
112
# Oops
You don't need quadrillions of dollars before you run into a value which
can't be represented in a floating point value to such a degree that the
rounding comes out wrong. Yes you could probably "fix" the rounding to make
it work in practice but the problems is that it is simply not possible to
represent the value 1.13 in a floating point variable exactly and when
dealing with money you want to represent it exacctly, particularly as it's
so easy to do.
More information about the Python-list
mailing list