ScaledDecimal for SIXX

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ScaledDecimal for SIXX

dario trussardi
Hi,

        i do some test for manage ScaledDecimal instances with SIXX into GLASS and Pharo.

        The first problem is the relative to the ScaledDecimal class definition:

        In Pharo is :

                Fraction subclass: #ScaledDecimal
                instanceVariableNames: 'scale'
                classVariableNames: ''
                poolDictionaries: ''
                category: 'Kernel-Numbers'


        In Gemstone is:

                Number subclass: 'ScaledDecimal'
                instVarNames: #( numerator denominator scale)
                classVars: #()
                classInstVars: #()
                poolDictionaries: #[]
                inDictionary: 'Globals'
                category: 'User Classes'
 
This is the first problem because ScaledDecimal class,  in GLASS don't know some method define in Fraction.
        ( createInstanceOf: aClass withSixxElement: sixxElement   / readSixxContentStringFrom: aStream  )

But change the hierarchy in GLASS  ,  i think is not  enough because i lose the scale of the ScaledDecimal  instance.

I have do this change to ScaledDecimal :

A)  For create SIXX reference:

        a1) sixxContentOn: aStream indent: level context: dictionary

                                                aStream nextPutAll:  self sixxString
               
        a2) sixxString

                " DTR rende stringa per SIXX (123/2s2) "
                ^ String streamContents:
                        [ :aStream |
                        aStream nextPut: $(.
                        numerator printOn: aStream "base: 10".
                        aStream nextPut: $/.
                        denominator printOn: aStream "base: 10".
                        aStream nextPut: $s.
                        scale printOn: aStream "base: 10".
                        aStream nextPut: $).
                                ]


B) For read data from SIXX file and create a new ScaledDecimal instance:

        b1) createInstanceOf: aClass withSixxElement: sixxElement
                        ^ self readSixxContentStringFrom: (ReadStream on: (SixxXmlUtil characterDataFrom: sixxElement))
       
        b2) readSixxContentStringFrom: aStream

                | numerator denominator scale |

                aStream next. "skip $("
                numerator := aStream upTo: $/ .
                denominator := aStream upTo: $s.
                scale := aStream upTo: $).
                ^ self numerator: numerator asNumber denominator: denominator asNumber scale: scale asNumber


These are my considerations,

        Any other pointers would be greatly appreciated !

       
        Dario


Reply | Threaded
Open this post in threaded view
|

Re: ScaledDecimal for SIXX

dario trussardi
Hi,

i have do some test about the problem.

The change i have do to ScaledDecimal class are:

         For class side :

        A) createInstanceOf: aClass withSixxElement: sixxElement
                        ^ self readSixxContentStringFrom: (ReadStream on: (SixxXmlUtil characterDataFrom: sixxElement))
       
        B) readSixxContentStringFrom: aStream

                | numerator denominator scale |

                aStream next. "skip $("
                numerator := aStream upTo: $/ .
                denominator := aStream upTo: $s.
                scale := aStream upTo: $).
                ^ self numerator: numerator asNumber denominator: denominator asNumber scale: scale asNumber
       

        For instance side:

        C) sixxContentOn: aStream indent: level context: dictionary
       
                        aStream nextPutAll: self sixxContentString
       
        D) sixxContentString

                ^ String streamContents: [:s | self storeOn: s]


        E) storeOn: aStream

                        aStream
                        nextPut: $(;
                  store: numerator;
                        nextPut: $/;
                        store: denominator;
                        nextPut: $s;
                        store: scale;
                        nextPut: $)

It work fine.


I have found problem in the SixxGeneratingTestCase where i add the test :

        testSixxStringForScaledDecimal
        | expectedStr |
        expectedStr :=  '<sixx.object sixx.id="0" sixx.type="ScaledDecimal" >(15432/125s3)</sixx.object>
'.
        self
                should: [123.456s3 sixxString = expectedStr]



Now my OODB run on MacBook and my Locale decimalPoint is set  to ','  ( comma )

        Now the:

                 123.456s3 sixxString

        or 123,456s3 sixxString

        are not right interpreted.
       
        The same problem when i do it in the Glass transcript.

       
Any idea about it ?






One other problem is relative to the asString.

If scale is set to 0 the string report is 123,

In this case i had

        " append 0 if not scale"
        scale = 0 ifTrue:[ aString nextPut: $0].

at the end of the method.

If scale is set to 0 the string report is now 123,0



asString

"Returns a String of the form '123.56 for a number with scale = 2."

" by OODB with change for Squeak compatibility : WriteStream "

" DTR append 0 after ,  if not scale "

| x num aString wholePart fraction |

aString :=  WriteStream on: ( String new ).
scale == nil ifTrue:[ ^ '(uninitialized ScaledDecimal)' "fix bug 13190"].
x := 10 raisedToInteger: scale .
num := ((numerator * x) + (denominator quo: 2)) // denominator.
(numerator < 0) ifTrue: [
    aString nextPut: $-.
    num := num negated.
    ].

wholePart := num // x.
fraction := num \\ x.

aString nextPutAll: (wholePart asString).
aString nextPutAll: (Locale decimalPoint). "fix 36666"
scale timesRepeat: [
  fraction := fraction * 10.
  aString nextPutAll: (fraction // x) asString.
  fraction := fraction \\ x.
  ].

" append 0 if not scale"
scale = 0 ifTrue:[ aString nextPut: $0].

^ aString contents




Anyone can generate a new Monticello version with integrations ?

I can do it ?

For other Dialect ( Pharo - VW ) how do new version ?


Any pointers would be greatly appreciated !

        Thanks,

                Dario