[ANN] FixedDecimals

nicolas cellier ncellier at ifrance.com
Tue Sep 4 20:44:30 UTC 2007


Interesting, however current version FixedDecimals.cbc.12.mcz is bugged.

example:

(-53/10 asFixedDecimal: 2) fractionPart.

Some suggestions:
- why maintaining so many forms in parallel ?
   Not bug-proof, you happen to update one form not the other...
   sign is not handled in part1 nor part2
- negated should create a new instance
   rather than modifying the number in place
- FixedDecimals does not implement hash,
   therefore cannot be stored in a Dictionary.
   Particularly, cannot be stored in a workspace variable.
   I suggest hash ^self asFraction hash
- likewise, implement all subclassResponsibility
	( 5/3) * (5/3 asFixedDecimal: 2).
	( 2.0s1) * (5/3 asFixedDecimal: 2).
	(5/3 asFixedDecimal: 2) reciprocal.
- coercion depends on receiver:
	| a b |
	a := (53/10 asFixedDecimal: 2).
	b := 5.3.
	{ (a+b) class.
	(b+a) class. } "is {FixedDecimal . Float}"
- maybe add another rounding mode in arithmetic (to nearest even...)
- create a specific notation for transparent read/write, like 5.30f
  (requires some easy support in Number class>>readFrom: or NumberParser)

 From algebraic point of view, the main concern is that some numbers 
might not have any reciprocal...
Unless the reciprocal is a Fraction, and arithmetic op coerce to Fraction...
But, eh, this is just the case of Float:
| f |
f := (8872240531573379/9007199254740992) asFloat.
f * f reciprocal - 1

Cheers

Nicolas

Chris Cunningham a écrit :
> Hi.
> 
> I've just release a package FixedDecimal on SqueakSource.
> 
> A FixedDecimal is similar to a ScaledDecimal, but different in certain 
> select ways.  It's primary purpose was to be able to represent precise 
> decimals for such things as representing money - where ScaledDecimals 
> leave something to be desired.  For instance, with ScaledDecimals, you get:
> (33.333s withScale:2) + (33.333s withScale:2)   print it yields 66.67s
> but with FixedDecimals, you would get:
> (33.333 asFixedDecimal: 2) + (33.333 asFixedDecimal: 2)    print it 
> yields 66.66.
> So, FixedDecimals round the numbers to the exact scale you specify - 
> converting a float to a FixedDecimal and back will not necessarily 
> return the starting number, unlike ScaledDecimals.
> 
> Most simple arithmetic is defined for FixedDecimals, but not all methods 
> that probably should be (it's getting late and I'm going away for the 
> weekend - thought I'd publish what I have).
> 
> Thanks,
> Chris
> 
> 
> ------------------------------------------------------------------------
> 
> 




More information about the Squeak-dev mailing list