Number.st
changeset 24996 79c4c3430535
parent 24988 2b353bb8b70a
child 25041 d6edd7fd2430
equal deleted inserted replaced
24995:1e0f6b44c6af 24996:79c4c3430535
   556         if sent to a concrete number-class, an instance of that class will be returned (independent of the exponent character)
   556         if sent to a concrete number-class, an instance of that class will be returned (independent of the exponent character)
   557     "
   557     "
   558 
   558 
   559     ^ [
   559     ^ [
   560         |value intValue mantissaAndScale scale decimalMantissa str
   560         |value intValue mantissaAndScale scale decimalMantissa str
   561          nextChar radix sign signExp exp numerator denom expChar fragment mantissa|
   561          nextChar radix sign signExp exp numerator denom expChar expChar2
       
   562          fragment mantissa|
   562 
   563 
   563         str := aStringOrStream readStream.
   564         str := aStringOrStream readStream.
   564 
   565 
   565         nextChar := str skipSeparators.
   566         nextChar := str skipSeparators.
   566         nextChar isNil ifTrue:[^ exceptionBlock value].
   567         nextChar isNil ifTrue:[^ exceptionBlock value].
   587                 nextChar := str peekOrNil.
   588                 nextChar := str peekOrNil.
   588             ].
   589             ].
   589             nextChar == $) ifFalse:[^ exceptionBlock value].
   590             nextChar == $) ifFalse:[^ exceptionBlock value].
   590             str next.
   591             str next.
   591             value := Fraction numerator:numerator denominator:denom.
   592             value := Fraction numerator:numerator denominator:denom.
   592             value := value * sign.
   593             sign < 0 ifTrue:[ value := value negated ].
   593             (self ~~ Number and:[self ~~ Fraction]) ifTrue:[
   594             (self ~~ Number and:[self ~~ Fraction]) ifTrue:[
   594                 value := self coerce:value.
   595                 value := self coerce:value.
   595             ].    
   596             ].    
   596             ^ value
   597             ^ value
   597         ].
   598         ].
   661             ].
   662             ].
   662 
   663 
   663             ('eEdDqQfF' includes:nextChar) ifTrue:[
   664             ('eEdDqQfF' includes:nextChar) ifTrue:[
   664                 expChar := nextChar.
   665                 expChar := nextChar.
   665                 nextChar := str nextPeekOrNil.
   666                 nextChar := str nextPeekOrNil.
       
   667                 expChar == $Q ifTrue:[
       
   668                     nextChar == $D ifTrue:[
       
   669                         expChar2 := nextChar.
       
   670                         nextChar := str nextPeekOrNil.
       
   671                     ] ifFalse:[
       
   672                         nextChar == $L ifTrue:[
       
   673                             expChar2 := nextChar.
       
   674                             nextChar := str nextPeekOrNil.
       
   675                         ]
       
   676                     ]
       
   677                 ].
   666 
   678 
   667                 signExp := 1.
   679                 signExp := 1.
   668                 (nextChar == $+) ifTrue:[
   680                 (nextChar == $+) ifTrue:[
   669                     nextChar := str nextPeekOrNil.
   681                     nextChar := str nextPeekOrNil.
   670                 ] ifFalse:[
   682                 ] ifFalse:[
   680                 "/      eEdD -> Float      (which is ieee-double)
   692                 "/      eEdD -> Float      (which is ieee-double)
   681                 "/      fF   -> ShortFloat (which is ieee-float)
   693                 "/      fF   -> ShortFloat (which is ieee-float)
   682                 
   694                 
   683                 self isAbstract ifTrue:[
   695                 self isAbstract ifTrue:[
   684                     ('qQ' includes:expChar) ifTrue:[
   696                     ('qQ' includes:expChar) ifTrue:[
   685                         value := value asLongFloat.
   697                         expChar2 == $D ifTrue:[
       
   698                             value := value asQDouble
       
   699                         ] ifFalse:[
       
   700                             expChar2 == $L ifTrue:[
       
   701                                 value := value asLargeFloat
       
   702                             ] ifFalse:[
       
   703                                 value := value asLongFloat.
       
   704                             ]
       
   705                         ]
   686                     ] ifFalse:[
   706                     ] ifFalse:[
   687                         ('fF' includes:expChar) ifTrue:[
   707                         ('fF' includes:expChar) ifTrue:[
   688                             value := value asShortFloat.
   708                             value := value asShortFloat.
   689                         ] ifFalse:[    
   709                         ] ifFalse:[    
   690                             "/ maybe in the far future we might create shortfloats when seeing eE,
   710                             "/ maybe in the far future we might create shortfloats when seeing eE,
  1250     ].
  1270     ].
  1251 
  1271 
  1252     self isAbstract ifFalse:[
  1272     self isAbstract ifFalse:[
  1253         value := (self coerce:intMantissa) / (self coerce:scaleFactor).
  1273         value := (self coerce:intMantissa) / (self coerce:scaleFactor).
  1254     ] ifTrue:[
  1274     ] ifTrue:[
       
  1275         "/ Float decimalPrecision
       
  1276         "/ LongFloat decimalPrecision
  1255         scale > 6 ifTrue:[
  1277         scale > 6 ifTrue:[
  1256             value := intMantissa asLongFloat / scaleFactor asLongFloat.
  1278             "/ scale > 19 ifTrue:[
       
  1279             "/     value := intMantissa asLargeFloat / scaleFactor asLargeFloat.
       
  1280             "/ ] ifFalse:[
       
  1281                 value := intMantissa asLongFloat / scaleFactor asLongFloat.
       
  1282             "/ ]
  1257         ] ifFalse:[
  1283         ] ifFalse:[
  1258             value := intMantissa asFloat / scaleFactor asFloat.
  1284             value := intMantissa asFloat / scaleFactor asFloat.
  1259         ].
  1285         ].
  1260     ].    
  1286     ].    
  1261     ^ (Array with:value with:intMantissa with:scale).
  1287     ^ (Array with:value with:intMantissa with:scale).
  1270      Number readMantissaAndScaleFrom:'01' readStream radix:10.
  1296      Number readMantissaAndScaleFrom:'01' readStream radix:10.
  1271      Number readMantissaAndScaleFrom:'001' readStream radix:10.
  1297      Number readMantissaAndScaleFrom:'001' readStream radix:10.
  1272      Number readMantissaAndScaleFrom:'0001' readStream radix:10.
  1298      Number readMantissaAndScaleFrom:'0001' readStream radix:10.
  1273      Number readMantissaAndScaleFrom:'000000000000000000000000000024' readStream radix:10.
  1299      Number readMantissaAndScaleFrom:'000000000000000000000000000024' readStream radix:10.
  1274      Number readMantissaAndScaleFrom:'0000000000000000000000000000000000000000000024' readStream radix:10.
  1300      Number readMantissaAndScaleFrom:'0000000000000000000000000000000000000000000024' readStream radix:10.
  1275      Number readMantissaAndScaleFrom:'123456789012345678901234567890' readStream radix:10.
  1301      Number readMantissaAndScaleFrom:'123456789012345678901234567890' readStream radix:10. 
       
  1302      Number readMantissaAndScaleFrom:'1234567890123456789012345678901234567890' readStream radix:10. 
  1276 
  1303 
  1277      Number readMantissaAndScaleFrom:'12345678901234567890' readStream radix:10.
  1304      Number readMantissaAndScaleFrom:'12345678901234567890' readStream radix:10.
  1278     "
  1305     "
  1279 
  1306 
  1280     "Modified: / 17-06-2017 / 03:03:03 / cg"
  1307     "Modified: / 17-06-2017 / 03:03:03 / cg"
  2321     "the minimum possible value taking me as a measurement with possible error;
  2348     "the minimum possible value taking me as a measurement with possible error;
  2322      as I am exact, that's myself"
  2349      as I am exact, that's myself"
  2323 
  2350 
  2324     ^ self
  2351     ^ self
  2325 ! !
  2352 ! !
       
  2353 
       
  2354 
       
  2355 
       
  2356 
  2326 
  2357 
  2327 !Number methodsFor:'printing & storing'!
  2358 !Number methodsFor:'printing & storing'!
  2328 
  2359 
  2329 displayOn:aGCOrStream
  2360 displayOn:aGCOrStream
  2330     "return a string to display the receiver.
  2361     "return a string to display the receiver.
  3937      such that (self truncated + self fractionPart) = self"
  3968      such that (self truncated + self fractionPart) = self"
  3938 
  3969 
  3939     ^ self - self truncated
  3970     ^ self - self truncated
  3940 
  3971 
  3941     "
  3972     "
  3942      1234.56789 fractionPart
  3973      1234.56789 fractionPart -- beware rounding errors in floats
  3943      1.2345e6 fractionPart
  3974      1.2345e6 fractionPart  
  3944 
  3975      (16/10) fractionPart
  3945      1.6 asLongFloat fractionPart + 1.6 asLongFloat truncated
  3976 
  3946      -1.6 asLongFloat fractionPart + -1.6 asLongFloat truncated
  3977      1.6 asLongFloat fractionPart + 1.6 asLongFloat truncated  -- beware rounding errors in floats 
       
  3978      -1.6 asLongFloat fractionPart + -1.6 asLongFloat truncated -1.600000000000000089
       
  3979      (16/10) fractionPart + (16/10) truncated -> 16/10
  3947     "
  3980     "
  3948 
  3981 
  3949     "Modified: / 4.11.1996 / 20:26:54 / cg"
  3982     "Modified: / 4.11.1996 / 20:26:54 / cg"
  3950     "Created: / 28.10.1998 / 17:14:40 / cg"
  3983     "Created: / 28.10.1998 / 17:14:40 / cg"
  3951 !
  3984 !
  3952 
  3985 
  3953 integerPart
  3986 integerPart
  3954     "return a float with value from digits before the decimal point
  3987     "return a float with value from digits before the decimal point
  3955      (i.e. the truncated value)"
  3988      (i.e. the truncated value)"
  3956 
  3989 
  3957     ^ self truncated asFloat
  3990     ^ self truncatedAsFloat
  3958 
  3991     "/ ^ self truncated asFloat
  3959     "
  3992 
  3960      1234.56789 integerPart
  3993     "
       
  3994      1234.56789 integerPart  
  3961      1.2345e6 integerPart
  3995      1.2345e6 integerPart
  3962      12.5 integerPart
  3996      12.5 integerPart
  3963      -12.5 integerPart
  3997      -12.5 integerPart
  3964      (5/3) integerPart
  3998      (5/3) integerPart
  3965      (-5/3) integerPart
  3999      (-5/3) integerPart
  3975     "Answer the float rounded with n digits of precision"
  4009     "Answer the float rounded with n digits of precision"
  3976 
  4010 
  3977     | mult |
  4011     | mult |
  3978 
  4012 
  3979     mult := 10 raisedTo: n.
  4013     mult := 10 raisedTo: n.
  3980     ^ (((self * mult) rounded) asFloat / mult).
  4014     ^ (((self * mult) rounded) asLimitedPrecisionReal / mult).
  3981 
  4015 
  3982     "
  4016     "
  3983      7 rounded:2
  4017      7 rounded:2
  3984      7.1 rounded:2
  4018      7.1 rounded:2
  3985      7.2345 rounded:2
  4019      7.2345 rounded:2  -> 7.23 
  3986      7.2385 rounded:2
  4020      7.2385 rounded:2  -> 7.24 
  3987      7.2341 rounded:3
  4021      7.2341 rounded:3  -> 7.234
  3988      7.2345 rounded:3
  4022      7.2345 rounded:3
  3989      7.2348 rounded:3
  4023      7.2348 rounded:3
       
  4024      (1/3) rounded:3   -> 0.333
       
  4025      (2/3) rounded:3   -> 0.667
  3990     "
  4026     "
  3991 ! !
  4027 ! !
  3992 
  4028 
  3993 !Number class methodsFor:'documentation'!
  4029 !Number class methodsFor:'documentation'!
  3994 
  4030