Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2001-05-09 13:06:50


> When J16 and WG21 begin working on specific proposals for the
> evolution of C++, I intend to suggest a fixed-point decimal type.
> The main reason is that many novices want to use floating-point
> types to store monetary values; and they're suprised when the
> pennies (or other fractional values) don't add up. In the
> traditional "business data processing" environment in which
> I work, this is often what Bjarne calls an "embarrassment."

Even fixed-point values suffer from:
  1) Binary representation (therefore causing approximate calculations when
dealing with decimal input, output, or arithmetic)
  2) Roundoff errors

Fixed-point arithmetic therefore shares several annoying attributes with
floating-point arithmetic; as an extreme example, assuming a 4-bit
underlying representation (ignoring signs) with the implied decimal in the
middle:
  0.1 + 0.1 != 0.2
(since there are only 2 bits for the values to the right of the decimal
point in our example, it can only represent .00, .25, .50, and .75;
therefore, "0.1" becomes equivalent to "0.00", and "0.2" becomes equivalent
to "0.25")

If the above seems too extreme, just consider dividing by 10, and realize
that an exact result can't be stored using a binary-encoded fixed-point
number. Do enough decimal calculations, and you'll eventually slip a digit.

I'm not against fixed-point arithmetic; I just don't think it solves the
problem.

If the intention is to help newbies by providing a class that behaves like
"textbook" (i.e., decimal) algebra, I'd rather see a C++ implementation of:
  http://www2.hursley.ibm.com/decimal/

        -Steve


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk