float equality

Kevin Bealer kevindangerbealer at removedanger.gmail.com
Mon Feb 21 03:37:24 PST 2011


== Quote from Walter Bright (newshound2 at digitalmars.com)'s article
> Kevin Bealer wrote:
> A reasonable way to do financial work is to use longs to represent pennies.
> After all, you don't have fractional cents in your accounts.
> Using floating point to represent money is a disaster in the making.
...
> There's just no getting around needing to understand how computer arithmetic works.
...
> I.e. you have to know what you're doing :-)

I'm sure that banks do what you are suggesting and have a policy (maybe a
regulatorily required one) on who gets the fractional cents.  This is easy because
cents are a standard that everyone agrees is the 'bottom'.

But there are lots of other cases that aren't standardized... i.e. is time
represented in seconds, milliseconds, nanoseconds?  Java, Windows and Linux use
different versions.  Some IBM computers measure time in 1/2^Nths of a second,
where N is around 20 or 30.  Is land measured in square feet or acres?

Whatever you pick as the bottom may not be good enough in the future.  If you
introduce more exact measurements, now you have mixed representations.  If you had
a rational number of acres, on the other hand, you could start doing measurements
in square feet and representing them as x/(square feet in an acre).

Mixed representations are trouble because they invite usage errors.  Rational
doesn't change the facts of life but it simplifies some things because it makes
the choice of base an arbitrary problem-domain kind of choice whereas in fixed
point it is very difficult to revisit this decision.

Every time I think about this, though, I think of "classroom" examples, so it's
probably the case that my arguments are academic (in the bad sense)... and I
should give it up.  If this was more useful it would probably be used more.

Kevin


More information about the Digitalmars-d mailing list