IEEEFloat.st
changeset 5363 1fa66e549cee
parent 5362 e3b499db0368
child 5364 1e9dbd3bab51
equal deleted inserted replaced
5362:e3b499db0368 5363:1fa66e549cee
    85      self fromFloat:0.5 asLongFloat
    85      self fromFloat:0.5 asLongFloat
    86     "
    86     "
    87 !
    87 !
    88 
    88 
    89 fromInteger:anInteger
    89 fromInteger:anInteger
    90     ^ self fromFloat:anInteger asFloat
    90     "convert to one of 64, 128 or 256 bit IEEEFloat"
       
    91 
       
    92     |nM nE h l fraction e bias absInt nBytes|
       
    93 
       
    94     absInt := anInteger abs.
       
    95     h := absInt highBit.
       
    96     l := absInt lowBit.
       
    97     nM := Float numBitsInMantissa.
       
    98     nE := Float numBitsInExponent.
       
    99     bias := Float eBias.
       
   100     nBytes := 64 / 8.
       
   101     (h - l) > nM ifTrue:[
       
   102         nM := QuadFloat numBitsInMantissa.
       
   103         nE := QuadFloat numBitsInExponent.
       
   104         bias := QuadFloat eBias.
       
   105         nBytes := 128 / 8.
       
   106         (h - l) > nM ifTrue:[
       
   107             nM := OctaFloat numBitsInMantissa.
       
   108             nE := OctaFloat numBitsInExponent.
       
   109             bias := OctaFloat eBias.
       
   110             nBytes := 256 / 8.
       
   111         ]
       
   112     ].
       
   113 
       
   114     "/ cut off precision by shifting right (if h > nM) 
       
   115     "/ or add zeros by shifting to the left 
       
   116     h == 0 ifTrue:[
       
   117         fraction := e := 0.
       
   118     ] ifFalse:[
       
   119         fraction := absInt bitShift:(nM - h + 1). 
       
   120         fraction := fraction bitAnd:((1 bitShift:nM)-1).
       
   121         e := bias + h - 1.
       
   122     ].
       
   123     ^ (self basicNew:nBytes) exponentSize:nE;
       
   124         setFraction:fraction exponent:e 
       
   125         signBit:(anInteger negative ifTrue:[1] ifFalse:[0]);
       
   126         yourself
    91 
   127 
    92     "
   128     "
    93      self fromInteger:1
   129      self fromInteger:1
       
   130      self fromInteger:-1
    94      self fromInteger:2
   131      self fromInteger:2
       
   132      self fromInteger:1024 * 1024 * 1024 * 1024 * 1024 * 1024
    95      self fromInteger:1e20 asInteger
   133      self fromInteger:1e20 asInteger
    96      self fromInteger:1e100 asInteger
   134      self fromInteger:1e100 asInteger
       
   135      self fromInteger:2r1010101010101010101010101010101
       
   136      self fromInteger:2r1010101010101010101010101010101010101010101010101010101010101010
       
   137      self fromInteger:1e-100 asInteger
       
   138      1 asIEEEFloat
    97     "
   139     "
    98 !
   140 !
    99 
   141 
   100 size:numBits exponentSize:exponentSize
   142 size:numBits exponentSize:exponentSize
   101     ^ (self basicNew:(numBits // 8)) exponentSize:exponentSize
   143     ^ (self basicNew:(numBits // 8)) exponentSize:exponentSize
   356      self assert:(IEEEFloat fromFloat:-2.0) negated = (IEEEFloat fromFloat:2.0) 
   398      self assert:(IEEEFloat fromFloat:-2.0) negated = (IEEEFloat fromFloat:2.0) 
   357     "
   399     "
   358 ! !
   400 ! !
   359 
   401 
   360 !IEEEFloat methodsFor:'coercing & converting'!
   402 !IEEEFloat methodsFor:'coercing & converting'!
       
   403 
       
   404 asIEEEFloat
       
   405     ^ self
       
   406 !
   361 
   407 
   362 coerce:aNumber
   408 coerce:aNumber
   363     "convert the argument aNumber into an instance of the receiver's class and return it.
   409     "convert the argument aNumber into an instance of the receiver's class and return it.
   364      Redefined to preserve the size and exponentSize"
   410      Redefined to preserve the size and exponentSize"
   365 
   411 
   972     "
  1018     "
   973      -1.0 asIEEEFloat negative  
  1019      -1.0 asIEEEFloat negative  
   974      0.0 asIEEEFloat negative   
  1020      0.0 asIEEEFloat negative   
   975      -0.0 asIEEEFloat negative  
  1021      -0.0 asIEEEFloat negative  
   976     "
  1022     "
   977 !
       
   978 
       
   979 positive
       
   980     "return true if the receiver is greater or equal to zero."
       
   981 
       
   982     ^ ((self basicAt:self basicSize) bitTest:16r80) not
       
   983 ! !
  1023 ! !
   984 
  1024 
   985 !IEEEFloat methodsFor:'truncation & rounding'!
  1025 !IEEEFloat methodsFor:'truncation & rounding'!
   986 
  1026 
   987 ceilingAsFloat
  1027 ceilingAsFloat