a decimal class for Squeak?

Hans-Martin Mosner hm.mosner at cityweb.de
Wed Jan 6 22:19:02 UTC 1999


Jan Bottorff wrote:

> At 09:50 PM 1/4/99 +0100, Hans-Martin Mosner wrote:
> >You probably don't need a decimal arithmetic class. What you need is
> >decimal fractions, which are just a special case of the good old
> >Fractions found in every self-respecting Smalltalk. With these,
> >arithmetic using +, -, *, / is exact (which is probably what you're after
> >anyway).
>
> For business math use, exact answers may NOT be what's needed. What's
> needed are consistent answers. You basically need control over the
> precision at every step of a computation.
>

Jan,
you're exactly right here. But having control over the precision means first to have
dependable arithmetic operations. Of course, if you calculate billing items, you'd
need to round intermediate results where appropriate. I had this kind of discussion
in my current project where leasing rates have to be calculated. The DLL used can be
set up to round to a given number of decimal places. Some people argued that the
higher number of decimal places would give more precise answers. Which is right in
the purely mathematical sense, but wrong in an accounting situation where you have
to bill the customer an integer number of {cent, penny, pfennig, ...} whatever your
smallest currency unit is. So by setting the rounding to 2 decimal digits, I get the
right results.

With fractions you have complete control over where rounding happens and how it is
performed. For example, rounding any positive number to 2 decimal places goes like
that:
(number * 100) rounded / 100
In the mentioned project, I simply implemented a method roundedToDecimalPlaces:
which does the right thing.
Scaled decimals make things unnecessarily complicated. Suppose you need to divide 10
by 3. How many decimal digits should the result have? You simply don't know at that
point, so rounding to any number of digits as an implicit step in the multiplication
is plain wrong.
Your DecimalMath example would allow me to control the rounding beforehand, but I
would consider it overkill when a simple method can do the right thing as well.

Hans-Martin





More information about the Squeak-dev mailing list