APM: Perl zero not being zero, but 7e-12

tmcd@panix.com tmcd at panix.com
Mon Sep 12 23:20:35 PDT 2005


I might or might not have started first, but my answer didn't hit the
list first.  Oh well.

On Tue, 13 Sep 2005, Mike South <msouth at gmail.com> wrote:
> If you are specifically dealing with money, one thing that business
> software will often do it store all values in pennies, so you won't
> (as easily) bring in error associated with floating point
> representations.

In my suggestion of the same thing, I should note that then you have
to decide how to handle roundoff yourself, ratherthan let the computer
hardware handle it.  For example, suppose you're selling something for
$123.45, which you can represent as 12345.  Suppose you need to add
sales tax of 8.25%.  The final value is $133.634625, which you cannot
represent.  If you store 13363, they pay $133.63 and you're not
charging $0.004625.  If you store 13354, they pay $133.64 and you're
overcharging $0.005375.  You may say that it doesn't matter, but
believe me, it does.  Sales tax laws may mandate one or the other, or
may not.  And accumulation of fractional pennies is a classic bank
programming fraud called "salami slicing": if you do it right, the
books balance but the programmer's account accumulates a lot of spare
change.

Short form again: don't expect exact computation using floating-point
numbers or your approximation to them.

-- 
Tim McDaniel; Reply-To: tmcd at panix.com


More information about the Austin mailing list