Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!oitnews.harvard.edu!purdue!lerc.nasa.gov!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!news.sprintlink.net!noc.netcom.net!netcomsv!uu3news.netcom.com!netcomsv!uucp3.netcom.com!medicus!billf
From: billf@medicus.com (Bill Foote)
Subject: Re: Rounding Error in Digitalk 3.0.1
Message-ID: <1995Jun22.224411.11493@medicus.com>
Organization: Medicus Systems Corp.
References: <3s6aa2$ssn@raffles.technet.sg> <1995Jun20.182314.24571@medicus.com> <3sbm7h$i76@raffles.technet.sg>
Date: Thu, 22 Jun 1995 22:44:11 GMT
Lines: 45

In article <3sbm7h$i76@raffles.technet.sg> UserName <user@technet.sg> writes:
>> The moral?  Be very careful when using floats, and never, *ever* use them
>> to represent money.  You have been warned :-)
>> 
>
>Thanks. In my system, I have plenty of formula in percentages that I need to
>multiply. You would actually recommend these formula be change to fixed integer
>computation to get precise result, then convert to decimal for precise 
>answers ?

I don't know of a decimal number class in Smalltalk (although I've only
used Digitalk and ParcPlace).  Remember, float is *not* a decimal type!

In VisualWorks (ParcPlace), there's a class called "FixedPoint" that should
be suitable for money.

Digitalk doesn't have this in their class library, but you could always use
Fraction, or just use Integer (with the understanding that everything is
in the smallest interesting denomination (probably either cents or mills in
the US)).  Fraction would also seem a reasonable choice for representing
percentages.  I've never used fractions extensively, but you'd probably
want to "round" them such that the denominator was 100 at the end of a
monetary computation.

Another option would be to write a FixedPoint class, or even a full-blown
Decimal class, but this would be a lot of work.  Perhaps there's something
on the Manchester goodies archive, or commercially?

>Is there any other ways ?

I've got a million of 'em :-)   Seriously, I'm sure there are many.  You
might want to search around for some class libraries that support 
financial-type stuff...  One of the things in any such class library
should be a good way to represent money.

>Does all financial system do these ?

No, but any financial system that naively expresses dollars (or any other
non-integral currency unit) as a float is broken!

--
Bill Foote                | Adde parvum parvo magnus acervus ecrit.
billf@medicus.com         | [Add little to little and there will be a big pile]
Medicus Systems           |    -- Ovid, via Frederick P. Brooks, Jr.
Alameda, CA USA           |
