Number.st
changeset 21828 d1a7e7c21694
parent 21815 e017f9904c30
child 21833 5276dd24e7c0
equal deleted inserted replaced
21827:a3f2ce15176f 21828:d1a7e7c21694
   563     "Modified (comment): / 08-06-2017 / 13:58:36 / mawalch"
   563     "Modified (comment): / 08-06-2017 / 13:58:36 / mawalch"
   564 ! !
   564 ! !
   565 
   565 
   566 !Number class methodsFor:'constants'!
   566 !Number class methodsFor:'constants'!
   567 
   567 
       
   568 e
       
   569     "return the closest approximation of the irrational number e"
       
   570 
       
   571     ^ self subclassResponsibility
       
   572 
       
   573     "Modified: / 16-06-2017 / 11:04:49 / cg"
       
   574 !
       
   575 
       
   576 epsilon
       
   577     "return the maximum relative spacing of instances of mySelf
       
   578      (i.e. the value-delta of the least significant bit)"
       
   579 
       
   580      ^ self subclassResponsibility
       
   581 !
       
   582 
       
   583 epsilonForCloseTo
       
   584     "return the epsilon used in the closeTo: comparison.
       
   585      (useful would be something like self epsilon or epsilon*10,
       
   586       but for Squeak compatibility.... - sigh)"
       
   587 
       
   588     ^ 0.0001
       
   589 
       
   590     "
       
   591      Float epsilon
       
   592      ShortFloat epsilon
       
   593      Float epsilon10
       
   594      ShortFloat epsilon10
       
   595     "
       
   596 !
       
   597 
       
   598 ln10
       
   599     "return ln(10) in my representation (and accuracy)."
       
   600 
       
   601     ^ self subclassResponsibility
       
   602 
       
   603     "Created: / 16-06-2017 / 11:00:38 / cg"
       
   604 !
       
   605 
       
   606 pi
       
   607     "return Pi in my representation (and accuracy)."
       
   608 
       
   609     ^ self subclassResponsibility
       
   610 
       
   611     "Modified (format): / 16-06-2017 / 11:00:42 / cg"
       
   612 ! !
       
   613 
       
   614 !Number class methodsFor:'constants & defaults'!
       
   615 
   568 decimalPointCharacter
   616 decimalPointCharacter
   569     "printed"
   617     "printed"
   570 
   618 
   571     <resource: #obsolete>
   619     <resource: #obsolete>
   572 
   620 
   672      Number decimalPointCharactersForReading:#( $. $,) .
   720      Number decimalPointCharactersForReading:#( $. $,) .
   673      Number fromString:'1.5'.
   721      Number fromString:'1.5'.
   674      Number fromString:'1,5'.
   722      Number fromString:'1,5'.
   675      Number decimalPointCharactersForReading:#( $. ).
   723      Number decimalPointCharactersForReading:#( $. ).
   676     "
   724     "
   677 !
       
   678 
       
   679 epsilon
       
   680     "return the maximum relative spacing of instances of mySelf
       
   681      (i.e. the value-delta of the least significant bit)"
       
   682 
       
   683      ^ self subclassResponsibility
       
   684 !
       
   685 
       
   686 epsilonForCloseTo
       
   687     "return the epsilon used in the closeTo: comparison.
       
   688      (useful would be something like self epsilon or epsilon*10,
       
   689       but for Squeak compatibility.... - sigh)"
       
   690 
       
   691     ^ 0.0001
       
   692 
       
   693     "
       
   694      Float epsilon
       
   695      ShortFloat epsilon
       
   696      Float epsilon10
       
   697      ShortFloat epsilon10
       
   698     "
       
   699 !
       
   700 
       
   701 pi
       
   702     "return Pi in my representation (and accuracy)."
       
   703 
       
   704      ^ self subclassResponsibility
       
   705 ! !
   725 ! !
   706 
   726 
   707 !Number class methodsFor:'error reporting'!
   727 !Number class methodsFor:'error reporting'!
   708 
   728 
   709 raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arg:arg errorString:text
   729 raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arg:arg errorString:text
   773 !Number class methodsFor:'private'!
   793 !Number class methodsFor:'private'!
   774 
   794 
   775 readMantissaAndScaleFrom:aStream radix:radix
   795 readMantissaAndScaleFrom:aStream radix:radix
   776     "helper for readFrom: -
   796     "helper for readFrom: -
   777      return the mantissa (post-decimal-point digits) from the (character-)stream aStream;
   797      return the mantissa (post-decimal-point digits) from the (character-)stream aStream;
   778      In addition, the scale (number of postDecimalPoint digits) is returned
   798      in addition, the mantissa as integer and the scale (number of postDecimalPoint digits) is returned
   779      (to support reading fixedPoint numbers).
   799      (both to support reading fixedPoint numbers and to not loose precision).
       
   800      The integer mantissa is needed as we do not yet know the target type (could be LongFloat or even QDouble).
   780      No whitespace is skipped.
   801      No whitespace is skipped.
   781      Errs if no number is available on aStream."
   802      Errs if no number is available on aStream."
   782 
   803 
   783     |nextChar value factor intMantissa scale digit|
   804     |nextChar value factor intMantissa scale digit scaleFactor xvalue|
   784 
   805 
   785     value := 0.0.
   806     value := 0.0.
   786     factor := 1.0 / radix.
   807     factor := 1.0 / radix.
       
   808     self isAbstract ifFalse:[    
       
   809         value := self zero.
       
   810         factor := self unity / (self coerce:radix).
       
   811     ].
   787     scale := 0.
   812     scale := 0.
       
   813     scaleFactor := 1.
   788     intMantissa := 0.
   814     intMantissa := 0.
   789     nextChar := aStream peekOrNil.
   815     nextChar := aStream peekOrNil.
   790     [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
   816     [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
   791 	digit := nextChar digitValue.
   817         digit := nextChar digitValue.
   792 	value := value + (digit * factor).
   818         scaleFactor := scaleFactor * radix.
   793 	intMantissa := (intMantissa * radix) + digit.
   819         value := value + (digit * factor).
   794 	factor := factor / radix.
   820         intMantissa := (intMantissa * radix) + digit.
   795 	scale := scale + 1.
   821         factor := factor / radix.
   796 	scale > 6 ifTrue:[
   822         scale := scale + 1.
   797 	    factor := factor asLongFloat.
   823 
   798 	    value := value asLongFloat.
   824         (scale > 6 and:[self isAbstract]) ifTrue:[
   799 	].
   825             factor := factor asLongFloat.
   800 	aStream next.
   826             value := value asLongFloat.
   801 	nextChar := aStream peekOrNil
   827         ].
   802     ].
   828         aStream next.
   803 
   829         nextChar := aStream peekOrNil
   804     ^ (Array with:value with:intMantissa with:scale).
   830     ].
       
   831 
       
   832     self isAbstract ifFalse:[
       
   833         xvalue := (self coerce:intMantissa) / (self coerce:scaleFactor).
       
   834     ] ifTrue:[
       
   835         scale > 6 ifTrue:[
       
   836             xvalue := intMantissa asLongFloat / scaleFactor asLongFloat.
       
   837         ] ifFalse:[
       
   838             xvalue := intMantissa asFloat / scaleFactor asFloat.
       
   839         ].
       
   840     ].    
       
   841     ^ (Array with:xvalue with:intMantissa with:scale).
   805 
   842 
   806     "
   843     "
   807      Number readMantissaAndScaleFrom:'234'    readStream radix:10.
   844      Number readMantissaAndScaleFrom:'234'    readStream radix:10.
   808      Number readMantissaAndScaleFrom:'2'      readStream radix:10.
   845      Number readMantissaAndScaleFrom:'2'      readStream radix:10.
   809      Number readMantissaAndScaleFrom:'234567' readStream radix:10.
   846      Number readMantissaAndScaleFrom:'234567' readStream radix:10.
   810      Number readMantissaAndScaleFrom:'234000' readStream radix:10.
   847      Number readMantissaAndScaleFrom:'234000' readStream radix:10.
   811      Number readMantissaAndScaleFrom:'234'    readStream radix:10.
   848      Number readMantissaAndScaleFrom:'234'    readStream radix:10.
       
   849      Number readMantissaAndScaleFrom:'000234' readStream radix:10.
       
   850      Number readMantissaAndScaleFrom:'000000000000000000000000000024' readStream radix:10.
       
   851      Number readMantissaAndScaleFrom:'123456789012345678901234567890' readStream radix:10.
   812 
   852 
   813      Number readMantissaAndScaleFrom:'12345678901234567890' readStream radix:10.
   853      Number readMantissaAndScaleFrom:'12345678901234567890' readStream radix:10.
   814     "
   854     "
   815 
   855 
   816     "Modified: / 14.4.1998 / 18:47:47 / cg"
   856     "Modified: / 17-06-2017 / 03:03:03 / cg"
   817 !
   857 !
   818 
   858 
   819 readMantissaFrom:aStream radix:radix
   859 readMantissaFrom:aStream radix:radix
   820     "helper for readFrom: -
   860     "helper for readFrom: -
   821      return the mantissa (post-decimal-point digits)
   861      return the mantissa (post-decimal-point digits)
  1452 
  1492 
  1453     ^ self log10
  1493     ^ self log10
  1454 !
  1494 !
  1455 
  1495 
  1456 log10
  1496 log10
  1457     "return log base 10 of the receiver"
  1497     "return log base-10 of the receiver.
       
  1498      Raises an exception, if the receiver is less or equal to zero.
       
  1499      Here, fallback to the general logarithm code."
  1458 
  1500 
  1459     (self isLimitedPrecisionReal not
  1501     (self isLimitedPrecisionReal not
  1460     or:[self generality < 1.0 generality]) ifTrue:[
  1502     or:[self generality < 1.0 generality]) ifTrue:[
  1461 	^ self asLongFloat log10.
  1503         ^ self asLongFloat log10.
  1462     ].
  1504     ].
  1463     ^ self log:10
  1505     ^ self ln / self class ln10
  1464 
  1506 
  1465     "
  1507     "
  1466 	(10 raisedTo:1000) log10
  1508      (10 raisedTo:1000) log10
  1467     "
  1509      (10 raisedTo:2000) log10
       
  1510      (10 raisedTo:4000) log10
       
  1511      (10 raisedTo:8000) log10
       
  1512     "
       
  1513 
       
  1514     "Modified (comment): / 16-06-2017 / 11:06:15 / cg"
  1468 !
  1515 !
  1469 
  1516 
  1470 log:aNumber
  1517 log:aNumber
  1471     "return log base aNumber of the receiver.
  1518     "return log base aNumber of the receiver.
  1472      This will usually return a float value"
  1519      This will usually return a float value"