Number.st
changeset 7388 bb89a53e2682
parent 7356 fe8fb0a571f2
child 7449 192789c6e1c2
equal deleted inserted replaced
7387:9be7990ded99 7388:bb89a53e2682
   345      In addition, the scale (number of postDecimalPoint digits) is returned 
   345      In addition, the scale (number of postDecimalPoint digits) is returned 
   346      (to support reading fixedPoint numbers).
   346      (to support reading fixedPoint numbers).
   347      No whitespace is skipped.
   347      No whitespace is skipped.
   348      Errs if no number is available on aStream."
   348      Errs if no number is available on aStream."
   349 
   349 
   350     |nextChar value factor intMantissa scale|
   350     |nextChar value factor intMantissa scale highPrecision|
   351 
   351 
       
   352     highPrecision := false.
   352     value := 0.0.
   353     value := 0.0.
   353     factor := 1.0 / radix.
   354     factor := 1.0 asLongFloat / radix.
   354     scale := 0.
   355     scale := 0.
   355     intMantissa := 0.
   356     intMantissa := 0.
   356     nextChar := aStream peekOrNil.
   357     nextChar := aStream peekOrNil.
   357     [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
   358     [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
   358         value := value + (nextChar digitValue * factor).
   359         value := value + (nextChar digitValue * factor).
   359         intMantissa := (intMantissa * radix) + nextChar digitValue.
   360         intMantissa := (intMantissa * radix) + nextChar digitValue.
   360         factor := factor / radix.
   361         factor := factor / radix.
   361         scale := scale + 1.
   362         scale := scale + 1.
   362         factor < (Float epsilon * 2) ifTrue:[
       
   363             value := value asLongFloat
       
   364         ].
       
   365         aStream next.
   363         aStream next.
   366         nextChar := aStream peekOrNil
   364         nextChar := aStream peekOrNil
   367     ].
   365     ].
       
   366 
   368     ^ (Array with:value with:intMantissa with:scale).
   367     ^ (Array with:value with:intMantissa with:scale).
   369 
   368 
   370     "
   369     "
   371      Number readMantissaAndScaleFrom:'234'    readStream radix:10. 
   370      Number readMantissaAndScaleFrom:'234'    readStream radix:10. 
   372      Number readMantissaAndScaleFrom:'2'      readStream radix:10. 
   371      Number readMantissaAndScaleFrom:'2'      readStream radix:10. 
   604 
   603 
   605 degreesToRadians
   604 degreesToRadians
   606     "interpreting the receiver as radians, return the degrees"
   605     "interpreting the receiver as radians, return the degrees"
   607 
   606 
   608     ^ (self * (Float pi)) / 180.0
   607     ^ (self * (Float pi)) / 180.0
       
   608 
       
   609     "
       
   610      180 degreesToRadians
       
   611      Float pi radiansToDegrees
       
   612     "
   609 !
   613 !
   610 
   614 
   611 literalArrayEncoding
   615 literalArrayEncoding
   612     "encode myself as an array literal, from which a copy of the receiver
   616     "encode myself as an array literal, from which a copy of the receiver
   613      can be reconstructed with #decodeAsLiteralArray."
   617      can be reconstructed with #decodeAsLiteralArray."
   620 
   624 
   621 radiansToDegrees
   625 radiansToDegrees
   622     "interpreting the receiver as degrees, return the radians"
   626     "interpreting the receiver as degrees, return the radians"
   623 
   627 
   624     ^ (self * 180.0) / (Float pi)
   628     ^ (self * 180.0) / (Float pi)
       
   629 
       
   630     "
       
   631      180 degreesToRadians     
       
   632      Float pi radiansToDegrees
       
   633     "
   625 !
   634 !
   626 
   635 
   627 withScale:newScale
   636 withScale:newScale
   628     "return a fixedPoint number representing the same valie as the receiver, 
   637     "return a fixedPoint number representing the same valie as the receiver, 
   629      with newScale number of post-decimal digits"
   638      with newScale number of post-decimal digits"
   893 ! !
   902 ! !
   894 
   903 
   895 !Number class methodsFor:'documentation'!
   904 !Number class methodsFor:'documentation'!
   896 
   905 
   897 version
   906 version
   898     ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.82 2003-06-16 09:17:12 cg Exp $'
   907     ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.83 2003-06-17 11:50:24 cg Exp $'
   899 ! !
   908 ! !