Storing floats, etc in GOODS

John Pierce john.raymond.pierce at gmail.com
Fri Oct 22 15:00:22 UTC 2004


Hi all,

> but I highly recommend if
> you're going to deal with Currency you develop a class (or classes) to
> support exact representations.

As a reminder, floating-point precision issues are not limited to when
you store floats in GOODS.  Print the following lines in a workspace:

1.35 roundTo: 0.1.
1.45 roundTo: 0.1.

Hmm, they both answer 1.4.  Not what I expected.  Now in my use of
Squeak in my work environment we actually did some work at building an
infinite precision decimal class and
quickly discovered the "ScaledDecimal" class after wandering down the
"build your own class" approach.  Great, right?

So we once we discovered ScaledDecimal we backed up and started
exploring this class, but didn't much like it's scaling factor which
seems to only be used for printing purposes.  The scaling factor
really did not factor into its math, only printing -- so we looked
further.

So upon further analysis of the implementation of the ScaledDecimal
class we saw that it merely was a Fraction with this weirdo scaling
factor that is only used for printing purposes.

Ah ha!  Squeak already has a perfectly adequate infinitely
precise decimal class.  It is called Fraction!  Now they print funny,
but (145 / 100) roundTo: 0.1 gives me the right answer!  Yippee!

So we thought, why not make all literal decimal values just be
Fractions instead of Floats?  How hard could it be?  Well, in fact, it
is far easier to just modify Squeak and use Fractions everywhere
instead of inventing your own Decimal class.

So attached are some change sets that a colleague of mine (Ben
Schroeder) and myself worked up that primarily modify your image to
use Fractions for literal decimals (e.g. 3.45 class -> Fraction).  We
don't remove the ability to use Floats, but you have to be more
explicit to get access to them (e.g. 3.45 asFloat class -> Float)

What's the upside of the attached changeset?
- Math works like math again (no issues with round error introduced by machine)
- You *don't* have to implement your own Currency class or Decimal
class to pick up infinite precision decimal math
- The parser turns literal values into fractions instead of floats
- Fractions print like decimals if the significand is less than 10
digits (as an arbitrary cut off), otherwise we print them like
Fractions usually do (e.g. (1/4) -> 0.25)
- We even re-compile all methods in your image that had literal floats
in them so we can be sure they are all using fractions.  We've run
this way for several months know with no issues -- go polymorphism!

What's the downside of the attached changeset?
- The ANSI compliance police will say this is not compliant with ANSI
and they are right.  Literal decimals are supposed to be represented
as IEEE Floats and not Fractions.
- If you depend on the speed of floating point math then using
Fractions instead will not be as performant as utilizing the FPU --
but for our business apps this has not been an issue
- External libraries that expect Floats should not be passed a
Fraction.  You *must* send asFloat to any fractions before being
passed external to Squeak.  This affects bitblt and Balloon3d and
other external libraries.  While the attached change sets address the
external libraries I mentioned, other's you may depend upon may
absolutely expect IEEE floats.

We've been running with this modification for over a month with no
side-effects on our image.  We prefer decimal math working like the
math we grew up with in grade-school.

We never submitted these changesets to the list because we feel this
would generate a lot of heated discussion about the correctness or
compliance or cross-platform issues.  We like the change for our local
work as we no longer have to deal with IEEE precision issues with the
math we do -- maybe you will enjoy that change too.  Let me know if
you need anything else.

John

-- 
If at first the idea is not absurd, then there is no hope for it. --
Albert Einstein
-------------- next part --------------
A non-text attachment was scrubbed...
Name: decimalfractions-bvs.1.cs
Type: application/text
Size: 6574 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20041022/8162134d/decimalfractions-bvs.1.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fractions.balloon.fixes-jrp.1.cs
Type: application/text
Size: 3112 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20041022/8162134d/fractions.balloon.fixes-jrp.1.bin


More information about the Squeak-dev mailing list