Stephen Waits
12/13/2005 7:06:00 AM
Joe Van Dyk wrote:
>
> What cracks can I lose money through?
Floating point numbers represent an extremely wide range of values -
much wider than their integer counterparts. This is handled through an
exponent and mantissa. For this ability, they trade off precision.
Think about the case of adding a large floating point number to a small
floating point number:
irb(main):001:0> a = 1.0e30
=> 1.0e+030
irb(main):002:0> b = 1.0e-30
=> 1.0e-030
irb(main):003:0> a + b
=> 1.0e+030
While this is an extreme example, it does demonstrate the loss of
precision. Essentially, in floating point arithmetic we're trying to
squeeze much more out of, say 32 or 48 or 64 bits.
Integer arithmetic, on the other hand, is exact. And therefore so is
fixed point arithmetic; however, fixed point doesn't enjoy the wide
representation range as floats.
The bottom line is you should never use floating point when it comes to
money. Eventually you're going to miss pennies. Instead represent
things in the smallest denomination, such as cents, and fix it up in
presentation, or use a custom Money column type, or data type.
There's your cracks! Just say no... unless you're a hot chick. Even
then it's questionable.
--Steve