FixedPoint.st
changeset 6894 64f27ed2753f
parent 6886 7acddeb0e526
child 6895 2434bee328c1
equal deleted inserted replaced
6893:9aa286d3a644 6894:64f27ed2753f
    13 "
    13 "
    14 
    14 
    15 "{ Package: 'stx:libbasic' }"
    15 "{ Package: 'stx:libbasic' }"
    16 
    16 
    17 Fraction subclass:#FixedPoint
    17 Fraction subclass:#FixedPoint
    18 	instanceVariableNames:'scale'
    18         instanceVariableNames:'scale'
    19 	classVariableNames:'PI_1000'
    19         classVariableNames:'PI_1000'
    20 	poolDictionaries:''
    20         poolDictionaries:''
    21 	category:'Magnitude-Numbers'
    21         category:'Magnitude-Numbers'
    22 !
    22 !
    23 
    23 
    24 FixedPoint comment:'
    24 FixedPoint comment:'
    25 Description: This class implements infinite precision fixed-point numbers. 
    25 Description: This class implements infinite precision fixed-point numbers. 
    26 It doesn''t really do anything too interesting except creating instances, converting, and printing, 
    26 It doesn''t really do anything too interesting except creating instances, converting, and printing, 
   164     "
   164     "
   165 !
   165 !
   166 
   166 
   167 numerator:n denominator:d scale:s
   167 numerator:n denominator:d scale:s
   168     ^ self basicNew
   168     ^ self basicNew
   169         setNumerator:n denominator:d scale:s;
   169         setNumerator:n denominator:d scale:s
   170         reduced
   170         "/ ; reduced
   171 !
   171 !
   172 
   172 
   173 readFrom:aStringOrStream 
   173 readFrom:aStringOrStream 
   174     "return the next FixedPoint from the (character-)stream aStream. 
   174     "return the next FixedPoint from the (character-)stream aStream. 
   175 
   175 
   187      FixedPoint readFrom:(ReadStream on:'foobar')     
   187      FixedPoint readFrom:(ReadStream on:'foobar')     
   188      FixedPoint readFrom:(ReadStream on:'foobar') onError:nil  
   188      FixedPoint readFrom:(ReadStream on:'foobar') onError:nil  
   189     "
   189     "
   190 
   190 
   191     "Modified: / 25.10.1997 / 15:30:29 / cg"
   191     "Modified: / 25.10.1997 / 15:30:29 / cg"
   192 !
       
   193 
       
   194 readFrom:aStringOrStream onError:exceptionBlock
       
   195     "return an instance of me as described on the string or stream, aStringOrStream.
       
   196      If an error occurs during conversion, return the result
       
   197      from evaluating exceptionBlock"
       
   198 
       
   199     | aStream sign integerPart fractionStream char fractionPart scale |
       
   200 
       
   201     aStream := aStringOrStream readStream.
       
   202 
       
   203     aStream peek == $- ifTrue:[
       
   204         sign := -1.
       
   205         aStream next.
       
   206     ] ifFalse:[
       
   207         sign := 1
       
   208     ].
       
   209 
       
   210     (aStream atEnd or:[aStream peek isLetter]) ifTrue: [^ exceptionBlock value].
       
   211 
       
   212     integerPart := (aStream upTo:$.) asNumber.
       
   213     (aStream atEnd or: [aStream peek isLetter]) ifTrue: [
       
   214         fractionPart := 0.
       
   215         scale := 1.
       
   216     ] ifFalse:[
       
   217         fractionStream := ReadWriteStream on:(String new: 10).
       
   218         [
       
   219             char := aStream next.
       
   220             char ~~ nil and:[char isDigit]
       
   221         ] whileTrue:[
       
   222             fractionStream nextPut:char
       
   223         ].
       
   224 
       
   225         scale := fractionStream positionStartingAt0.
       
   226         fractionStream reset.
       
   227         fractionPart := Number readFrom:fractionStream.
       
   228     ].
       
   229 
       
   230     ^ self basicNew 
       
   231         setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign
       
   232         scale:scale
       
   233 
       
   234     "
       
   235      FixedPoint readFrom:'123.456'
       
   236      FixedPoint readFrom:'-123.456' 
       
   237      FixedPoint readFrom:'123' 
       
   238      FixedPoint readFrom:'-123' 
       
   239      FixedPoint readFrom:'-123.abcd' onError:[47.5] 
       
   240      FixedPoint readFrom:'-1a.bcd' onError:[47.5] 
       
   241      FixedPoint readFrom:'foot' onError:['bad fixedpoint'] 
       
   242     "
       
   243 
       
   244     "Created: / 25.10.1997 / 15:28:59 / cg"
       
   245     "Modified: / 25.10.1997 / 15:31:47 / cg"
       
   246 ! !
   192 ! !
   247 
   193 
   248 !FixedPoint class methodsFor:'constants'!
   194 !FixedPoint class methodsFor:'constants'!
   249 
   195 
   250 pi
   196 pi
   261      self pi reciprocal 
   207      self pi reciprocal 
   262      1 / self pi 
   208      1 / self pi 
   263      self pi * 1000000000000000000000000000000000000000000    
   209      self pi * 1000000000000000000000000000000000000000000    
   264     "
   210     "
   265 ! !
   211 ! !
       
   212 
   266 
   213 
   267 !FixedPoint methodsFor:'accessing'!
   214 !FixedPoint methodsFor:'accessing'!
   268 
   215 
   269 scale
   216 scale
   270     "return the number of places of significance that is carried by the receiver."
   217     "return the number of places of significance that is carried by the receiver."
   369 !
   316 !
   370 
   317 
   371 + aNumber
   318 + aNumber
   372     "return the sum of the receiver and the argument, aNumber.
   319     "return the sum of the receiver and the argument, aNumber.
   373      Redefined to care for the scale if the argument is another fixPoint number.
   320      Redefined to care for the scale if the argument is another fixPoint number.
   374      The resulting scale will be the maximum of the receivers and the
   321      The results scale will be the maximum of the receivers and the
   375      arguments scale."
   322      arguments scale."
   376 
   323 
   377     |n d|
   324     |n d|
   378 
   325 
   379     (aNumber isInteger or:[aNumber isFraction]) ifTrue:[
   326     (aNumber isInteger or:[aNumber isFraction]) ifTrue:[
   568      The results scale is the maximum of the receivers scale and the arguments
   515      The results scale is the maximum of the receivers scale and the arguments
   569      scale."
   516      scale."
   570 
   517 
   571     aNumber isInteger ifTrue:[
   518     aNumber isInteger ifTrue:[
   572         ^ self class 
   519         ^ self class 
   573            numerator:numerator
   520                 numerator:numerator
   574            denominator:(denominator * aNumber)
   521                 denominator:(denominator * aNumber)
   575            scale:scale
   522                 scale:scale
   576     ].
   523     ].
   577 
   524 
   578     aNumber isFraction ifTrue:[
   525     aNumber isFraction ifTrue:[
   579         ^ self class 
   526         ^ self class 
   580             numerator:(numerator * aNumber denominator) 
   527             numerator:(numerator * aNumber denominator) 
   635      Transcript show:'float (inexact): '; showCR:(1 / 0.9999999).
   582      Transcript show:'float (inexact): '; showCR:(1 / 0.9999999).
   636     "
   583     "
   637 !
   584 !
   638 
   585 
   639 negated
   586 negated
   640     "have to redefine it from Fraction"
   587     "redefined from Fraction to preserve scale"
   641 
   588 
   642     ^ self class 
   589     ^ self class 
   643         numerator:(numerator negated)
   590         numerator:(numerator negated)
   644         denominator:denominator
   591         denominator:denominator
   645         scale:scale
   592         scale:scale
   646 !
   593 !
   647 
   594 
   648 reciprocal
   595 reciprocal
   649     "have to redefine it from Fraction"
   596     "redefined from Fraction to preserve scale"
   650 
   597 
   651     numerator == 1 ifTrue:[^ denominator].
   598     numerator == 1 ifTrue:[^ denominator].
   652     ^ self class 
   599     ^ self class 
   653         numerator:denominator
   600         numerator:denominator
   654         denominator:numerator
   601         denominator:numerator
   941     scale := newScale.
   888     scale := newScale.
   942 
   889 
   943     "Modified: 12.4.1997 / 11:22:02 / cg"
   890     "Modified: 12.4.1997 / 11:22:02 / cg"
   944 ! !
   891 ! !
   945 
   892 
       
   893 
   946 !FixedPoint methodsFor:'testing'!
   894 !FixedPoint methodsFor:'testing'!
   947 
   895 
   948 isFixedPoint
   896 isFixedPoint
   949     "return true, if the receiver is some kind of fixedPoint number;
   897     "return true, if the receiver is some kind of fixedPoint number;
   950      true is returned here - the method is redefined from Object."
   898      true is returned here - the method is redefined from Object."
   955 ! !
   903 ! !
   956 
   904 
   957 !FixedPoint class methodsFor:'documentation'!
   905 !FixedPoint class methodsFor:'documentation'!
   958 
   906 
   959 version
   907 version
   960     ^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.21 2002-11-25 09:19:48 stefan Exp $'
   908     ^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.22 2002-11-26 10:41:25 cg Exp $'
   961 ! !
   909 ! !