IEEEFloat.st
changeset 5342 ff3fcae67b03
parent 5341 a5a32ee87cbc
child 5345 81044fb6e21b
equal deleted inserted replaced
5341:a5a32ee87cbc 5342:ff3fcae67b03
    22     funny sized floating point numbers (such as float24 or float8) from
    22     funny sized floating point numbers (such as float24 or float8) from
    23     external sources, or to simulate computations on otherwise unsupported floating pnt numbers.
    23     external sources, or to simulate computations on otherwise unsupported floating pnt numbers.
    24 
    24 
    25     Create one by passing the overall number of bits and the number of exponent-bits:
    25     Create one by passing the overall number of bits and the number of exponent-bits:
    26         IEEEFloat size:64 exponentSize:(Float numBitsInExponent)
    26         IEEEFloat size:64 exponentSize:(Float numBitsInExponent)
       
    27     or:
       
    28         1.0 asIEEEFloat
    27     will give you a (slow) variant of a regular float.
    29     will give you a (slow) variant of a regular float.
    28 
    30 
    29     And:
    31     And:
    30         IEEEFloat size:256 exponentSize:19
    32         IEEEFloat size:256 exponentSize:19
    31     will give you a 256 bit ieee float.
    33     will give you a 256 bit ieee float.
   314     "
   316     "
   315 ! !
   317 ! !
   316 
   318 
   317 !IEEEFloat methodsFor:'coercing & converting'!
   319 !IEEEFloat methodsFor:'coercing & converting'!
   318 
   320 
       
   321 coerce:aNumber
       
   322     "convert the argument aNumber into an instance of the receiver's class and return it.
       
   323      Redefined to preserve the size and exponentSize"
       
   324 
       
   325     aNumber isInteger ifTrue:[
       
   326         ^ (self class basicNew:self basicSize)
       
   327             exponentSize:exponentSize; 
       
   328             setFraction:aNumber abs exponent:0 
       
   329             signBit:(aNumber negative ifTrue:[1] ifFalse:[0]);
       
   330             yourself
       
   331     ].
       
   332     ^ self class coerce:aNumber
       
   333 
       
   334     "Modified: / 15-06-2017 / 10:27:03 / cg"
       
   335 !
       
   336 
   319 generality
   337 generality
   320     ^ 97   "/ between OctaFloat and LargeFloat
   338     ^ 97   "/ between OctaFloat and LargeFloat
   321 
   339 
   322     "
   340     "
   323      1 asShortFloat generality 70
   341      1 asShortFloat generality 70
   385 
   403 
   386     ^ true.
   404     ^ true.
   387 !
   405 !
   388 
   406 
   389 lessFromIEEEFloat:anIEEEFloat
   407 lessFromIEEEFloat:anIEEEFloat
   390     ||
   408     |m1 m2 nM1 nM2 e1 e2|
       
   409 
       
   410     self negative = anIEEEFloat negative ifFalse:[
       
   411         ^ anIEEEFloat negative
       
   412     ].
   391 
   413 
   392     anIEEEFloat basicSize == self basicSize ifTrue:[
   414     anIEEEFloat basicSize == self basicSize ifTrue:[
   393     ].
   415         anIEEEFloat exponentSize == exponentSize ifTrue:[
   394     self halt.
   416             self basicSize to:1 by:-1 do:[:i |
       
   417                 (anIEEEFloat basicAt:i) < (self basicAt:i) ifTrue:[^ true].
       
   418                 (anIEEEFloat basicAt:i) > (self basicAt:i) ifTrue:[^ false].
       
   419             ].
       
   420             ^ false.
       
   421         ].
       
   422     ].
       
   423 
       
   424     "/ more complicated compare
       
   425 
       
   426     "/ bring to same exponent, add m1 + m2, normalize
       
   427 
       
   428     m1 := anIEEEFloat mantissaBits. 
       
   429     m2 := self mantissaBits.        
       
   430 
       
   431     nM1 := anIEEEFloat numBitsInMantissa.
       
   432     nM2 := self numBitsInMantissa.
       
   433     nM1 ~= nM2 ifTrue:[
       
   434         nM1 > nM2 ifTrue:[
       
   435             m2 := m2 bitShift:(nM1-nM2).
       
   436         ] ifFalse:[
       
   437             m1 := m1 bitShift:(nM2-nM1).
       
   438         ].
       
   439     ].
       
   440 
       
   441     e1 := anIEEEFloat exponent.
       
   442     e2 := self exponent.
       
   443     e1 = e2 ifTrue:[^ m1 < m2].
       
   444     ^ e1 < e2.
   395 !
   445 !
   396 
   446 
   397 productFromIEEEFloat:anIEEEFloat
   447 productFromIEEEFloat:anIEEEFloat
   398     "/ anIEEEFloat * self
   448     "/ anIEEEFloat * self
   399 
   449 
   646     ^ #()
   696     ^ #()
   647 ! !
   697 ! !
   648 
   698 
   649 !IEEEFloat methodsFor:'printing & storing'!
   699 !IEEEFloat methodsFor:'printing & storing'!
   650 
   700 
   651 displayOn:aStream
       
   652 aStream nextPutAll:'IEEEFloat'.
       
   653 ^ self.
       
   654 !
       
   655 
       
   656 displayString
       
   657 ^ 'IEEEFloat'.
       
   658 
       
   659 !
       
   660 
       
   661 printOn:aStream
   701 printOn:aStream
   662 aStream nextPutAll:'IEEEFloat'.
       
   663 ^ self.
       
   664     thisContext isRecursive ifTrue:[
   702     thisContext isRecursive ifTrue:[
   665         aStream nextPutAll:'IEEEFloat (error while printing)'.
   703         aStream nextPutAll:'IEEEFloat (error while printing)'.
   666         ^ self.
   704         ^ self.
   667     ].
   705     ].
   668 
   706     "/ super printOn:aStream.
   669     PrintfScanf printf:'%g' on:aStream argument:self.
   707     PrintfScanf printf:'%g' on:aStream argument:self.
   670 !
       
   671 
       
   672 printOn:aStream base:b
       
   673 aStream nextPutAll:'IEEEFloat'.
       
   674 !
       
   675 
       
   676 printOn:aStream base:b showRadix:showRadix
       
   677 aStream nextPutAll:'IEEEFloat'.
       
   678 !
       
   679 
       
   680 printOn:aStream thousandsSeparator:thousandsSeparator
       
   681 aStream nextPutAll:'IEEEFloat'.
       
   682 !
       
   683 
       
   684 printString
       
   685 ^ 'IEEEFloat'.
       
   686 !
       
   687 
       
   688 printStringFormat:formatString
       
   689 ^ 'IEEEFloat'.
       
   690 !
       
   691 
       
   692 printStringRadix:base
       
   693 ^ 'IEEEFloat'.
       
   694 !
       
   695 
       
   696 printStringRadix:base showRadix:showRadixBoolean
       
   697 ^ 'IEEEFloat'.
       
   698 !
       
   699 
       
   700 printStringScientific
       
   701 ^ 'IEEEFloat'.
       
   702 !
       
   703 
       
   704 printfPrintString:formatString
       
   705 ^ 'IEEEFloat'.
       
   706 ! !
   708 ! !
   707 
   709 
   708 !IEEEFloat methodsFor:'queries'!
   710 !IEEEFloat methodsFor:'queries'!
   709 
   711 
   710 eBias
   712 eBias
   746      bits are available in the mantissa (the hidden bit is not counted here):
   748      bits are available in the mantissa (the hidden bit is not counted here):
   747         s ee...ee mmm...mmm
   749         s ee...ee mmm...mmm
   748     "
   750     "
   749 
   751 
   750     ^ (self basicSize * 8) - exponentSize - 1 "/ sign
   752     ^ (self basicSize * 8) - exponentSize - 1 "/ sign
       
   753 !
       
   754 
       
   755 radix
       
   756     "Answer the exponent's radix"
       
   757 
       
   758     ^ 2
   751 ! !
   759 ! !
   752 
   760 
   753 !IEEEFloat methodsFor:'testing'!
   761 !IEEEFloat methodsFor:'testing'!
   754 
   762 
   755 isFinite
   763 isFinite