[comp.lang.smalltalk] Smalltalk Numerics

bart@reed.UUCP (Bart Massey) (07/18/88)

In article <4090@rpp386.UUCP> jfh@rpp386.UUCP (The Beach Bum) writes:
> In article <198@skep2.ATT.COM> wcs@skep2.UUCP (46323-Bill.Stewart.[ho95c],2G218,x0705,) writes:
> >In article <442@laura.UUCP> georg@unido.UUCP (Georg A. M. Heeg) writes:
> >:As basic technology we need decimal fixed point arithmetic.
> >:The fiscal office certainly does not accept rounding errors through
> >:floating point arithmetic....
> >
> >Surprise!  You'll get roundoff errors just as badly in fixed decimal,
> >if you ever do interest rates or division.
> 
> this is cobol's domain... [discussion]

Of course, by the time you read this, someone else will have said it, but
the right answer is:  Smalltalk does the right thing for you!  See "The
Numeric Classes" in ST:TLAII for the details, but the basic idea is this:
The result of multiplying, adding, or subtracting two integers is always
guaranteed to be the correct integer (regardless of size!).  The division of
two integers yields an object of class Fraction, which is simply a
representation of the division as a "reduced" fraction (i.e. a quotient of
two integers).  Further arithmetic on an instance of Fraction will preserve
the fractional property.  Thus, you will never accidentally lose precision
in MDAS arithmetic in Smalltalk, except possibly in printing.  However,
there is a class Float, which is used for things like the results of trig
functions, and this class is "sticky" -- operating with a Float and another
number will in general produce a Float.  So you have to be careful to either
(a) stick with 4-function math (okay for most accounting) or (b) put
asFraction in appropriate spots (see ref. for details).

But all of this may miss the point.  From what (very!!) little I understand
of standard accounting practice, perfect accuracy will produce incorrect
results!  Instead, the correct approach is to round to the nearest "mil" in
certain standard places(?).  One can do this with Integer and Fraction
classes in the obvious fashion as well, producing an Integer for further
calculations.

Note the standard features of Smalltalk arithmetic: no loss of precision
where at all avoidable, coercion where appropriate and nowhere else.  The
numeric stuff is one of Smalltalk's coolest features, in my opinion.

					Bart Massey
					UUCP: ..tektronix!reed!bart