[Pharo-project] SIXX problem for ScaledDecimal

Dario Trussardi dario.trussardi at tiscali.it
Mon May 23 18:51:02 CEST 2011


> No, no, no.
> Don't use Float.
> Float are INEXACT
> 
> (( 0.33333 asScaledDecimal: 5 )+  (0.33333 asScaledDecimal: 5 ) ) *6 = 3.99996s5
>  -> FALSE !
> 
> Nicolas

> I don't know what ScaledDecimal expectations are or should be.
> By now, you are in charge or performing the rounding operation by yourself
> 
> ( (1/3) asScaledDecimal: 2) roundTo: 0.01s2.
> 
> We may add more convenient #roundToPrecision, and #roundToPrecision:
> But you could also truncate, fllor or ceil...
> 
> Nicolas

> On Fri, May 20, 2011 at 7:57 AM, Ralph Boland <rpboland at gmail.com> wrote:
>> Dealing with ScaledDecimal is a complicated matter and I have probably failed
>> to appreciate many of the complications but whoever is implementing
>> ScaledDecimal
>> (and Smalltalk needs ScaledDecimal) needs to appreciate those complications.
>> Anybody here a financial expert?
>> 
> 
> I am not a financial expert, but this is how ScaledDecimal works, in
> both Squeak/Pharo and I believe other implementations, too.
> 
> I did run into this previously and it bugged me enough that I created
> FixedDecimal (at http://www.squeaksource.com/FixedDecimal.html ). It
> does not keep an internal more precise number - it keeps exactly what
> it shows you.  (Although the implementation is somewhat odd - it does
> do what it is supposed to do.)
> 
> -Chris
> 

After Your considerations i do this conclusion for manage ScaledDecimal instance with SIXX.

1) the instance reference into the SIXX declaration is based on the ScaledDecimal notation :  1234.333s3 

	 '<sixx.object sixx.id="0" sixx.type="ScaledDecimal" >1234.333s3</sixx.object>
'

2) All the translation is based on it.




Other think is the internal ScaledDecimal represent.

It's important to rounding the data so that the		 numerator  and 	denominator    are integer.

I do it wtih method :
	
	roundToPrecision: scalePrecision

	| rnd | 

	rnd:= ScaledDecimal new 
				setNumerator: 1
				denominator: (10 raisedTo: scalePrecision)
				scale: scalePrecision.
	
	^self roundTo: rnd



The round is automatic when i create ScaledDecimal instance with: 	 fromString: aString 


	For example when converter a input string to ScaledDecimal instance based on number of digit typed.


	It's required when i do conversion from another Number type:

	( 1/3 asScaledDecimal: 3 ) roundToPrecision


It's all for now.

	Any pointers would be greatly appreciated !

	Thanks,

		Dario






More information about the Pharo-project mailing list