Number.st
changeset 23615 3faa6422e9b7
parent 23481 6a64d7b9df9f
child 23619 90c9530305b7
equal deleted inserted replaced
23608:4b263e7e24c1 23615:3faa6422e9b7
   245      Return the value of exceptionBlock, if no number can be read.
   245      Return the value of exceptionBlock, if no number can be read.
   246      This method is less strict than the Smalltalk number reader; 
   246      This method is less strict than the Smalltalk number reader; 
   247      it allows for prefixed + and also allows missing fractional part after eE.
   247      it allows for prefixed + and also allows missing fractional part after eE.
   248      It supports 0x, 0o and 0b prefixes (hex, octal and binary)
   248      It supports 0x, 0o and 0b prefixes (hex, octal and binary)
   249      and the regular Smalltalk radix prefix xr.
   249      and the regular Smalltalk radix prefix xr.
       
   250      If also allows for strings like '1.0×1015' to be read (as 1E+15).
       
   251 
   250      It also allows garbage after the number - i.e. it reads what it can.
   252      It also allows garbage after the number - i.e. it reads what it can.
   251      See #fromString: , which is more strict and does not allow garbage at the end.
   253      See #fromString: , which is more strict and does not allow garbage at the end.
   252 
   254 
   253      Notice (see examples below): 
   255      Notice (see examples below): 
   254         if sent to Number, it will decide which type of number to return (depending on the exponent character);
   256         if sent to Number, it will decide which type of number to return (depending on the exponent character);
   432                                     numerator:(intValue * denom) + (mantissaAndScale second)
   434                                     numerator:(intValue * denom) + (mantissaAndScale second)
   433                                     denominator:denom
   435                                     denominator:denom
   434                                     scale:(scale ? mantissaAndScale third).
   436                                     scale:(scale ? mantissaAndScale third).
   435                     ].
   437                     ].
   436                 ] ifFalse:[
   438                 ] ifFalse:[
   437                     (self inheritsFrom:LimitedPrecisionReal) ifTrue:[
   439                     (nextChar == $×) ifTrue:[
   438                         "when requesting a specific Float instance, coerce it.
   440                         (((nextChar := str nextPeek) == $1)
   439                          otherwise return a value without loosing precision"
   441                           and:[ ((nextChar := str nextPeek) == $0) ]
   440                         value := self coerce:value.
   442                         ) ifTrue:[
       
   443                             str next.
       
   444                             exp := (Integer readFrom:str).
       
   445                             value := value * ((value class unity * 10.0) raisedToInteger:exp).
       
   446                         ] ifFalse:[
       
   447                             ^ exceptionBlock value.
       
   448                         ].
       
   449                     ] ifFalse:[        
       
   450                         (self inheritsFrom:LimitedPrecisionReal) ifTrue:[
       
   451                             "when requesting a specific Float instance, coerce it.
       
   452                              otherwise return a value without loosing precision"
       
   453                             value := self coerce:value.
       
   454                         ].
   441                     ].
   455                     ].
   442                 ].
   456                 ].
   443             ].
   457             ].
   444         ].
   458         ].
   445         sign == -1 ifTrue:[
   459         sign == -1 ifTrue:[
   502      DecimalPointCharactersForReading := #( $. ).
   516      DecimalPointCharactersForReading := #( $. ).
   503      Number readFrom:'99,00'
   517      Number readFrom:'99,00'
   504     "
   518     "
   505 
   519 
   506     "Created: / 27-10-2018 / 09:21:11 / Claus Gittinger"
   520     "Created: / 27-10-2018 / 09:21:11 / Claus Gittinger"
       
   521     "Modified (comment): / 18-01-2019 / 12:34:40 / Claus Gittinger"
   507 !
   522 !
   508 
   523 
   509 readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
   524 readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
   510     "return the next Number from the (character-)stream aStream;
   525     "return the next Number from the (character-)stream aStream;
   511      skipping all whitespace first.
   526      skipping all whitespace first.
  1581      t2 := 1.5 differenceFromTimestamp:t1.
  1596      t2 := 1.5 differenceFromTimestamp:t1.
  1582      t1 inspect. t2 inspect.
  1597      t1 inspect. t2 inspect.
  1583     "
  1598     "
  1584 ! !
  1599 ! !
  1585 
  1600 
       
  1601 
  1586 !Number methodsFor:'intervals'!
  1602 !Number methodsFor:'intervals'!
  1587 
  1603 
  1588 downTo:stop
  1604 downTo:stop
  1589     "return an interval from receiver down to the argument, incrementing by -1"
  1605     "return an interval from receiver down to the argument, incrementing by -1"
  1590 
  1606