#REFACTORING by exept
class: IEEEFloat
added:
#coerce:
#radix
removed:
#displayOn:
#displayString
#printOn:base:
#printOn:base:showRadix:
#printOn:thousandsSeparator:
#printString
#printStringFormat:
#printStringRadix:
#printStringRadix:showRadix:
#printStringScientific
#printfPrintString:
comment/format in:
#lessFromIEEEFloat:
#printOn:
class: IEEEFloat class
comment/format in: #documentation
--- a/IEEEFloat.st Mon Dec 02 08:55:03 2019 +0100
+++ b/IEEEFloat.st Mon Dec 02 09:58:36 2019 +0100
@@ -24,6 +24,8 @@
Create one by passing the overall number of bits and the number of exponent-bits:
IEEEFloat size:64 exponentSize:(Float numBitsInExponent)
+ or:
+ 1.0 asIEEEFloat
will give you a (slow) variant of a regular float.
And:
@@ -316,6 +318,22 @@
!IEEEFloat methodsFor:'coercing & converting'!
+coerce:aNumber
+ "convert the argument aNumber into an instance of the receiver's class and return it.
+ Redefined to preserve the size and exponentSize"
+
+ aNumber isInteger ifTrue:[
+ ^ (self class basicNew:self basicSize)
+ exponentSize:exponentSize;
+ setFraction:aNumber abs exponent:0
+ signBit:(aNumber negative ifTrue:[1] ifFalse:[0]);
+ yourself
+ ].
+ ^ self class coerce:aNumber
+
+ "Modified: / 15-06-2017 / 10:27:03 / cg"
+!
+
generality
^ 97 "/ between OctaFloat and LargeFloat
@@ -387,11 +405,43 @@
!
lessFromIEEEFloat:anIEEEFloat
- ||
+ |m1 m2 nM1 nM2 e1 e2|
+
+ self negative = anIEEEFloat negative ifFalse:[
+ ^ anIEEEFloat negative
+ ].
anIEEEFloat basicSize == self basicSize ifTrue:[
+ anIEEEFloat exponentSize == exponentSize ifTrue:[
+ self basicSize to:1 by:-1 do:[:i |
+ (anIEEEFloat basicAt:i) < (self basicAt:i) ifTrue:[^ true].
+ (anIEEEFloat basicAt:i) > (self basicAt:i) ifTrue:[^ false].
+ ].
+ ^ false.
+ ].
].
- self halt.
+
+ "/ more complicated compare
+
+ "/ bring to same exponent, add m1 + m2, normalize
+
+ m1 := anIEEEFloat mantissaBits.
+ m2 := self mantissaBits.
+
+ nM1 := anIEEEFloat numBitsInMantissa.
+ nM2 := self numBitsInMantissa.
+ nM1 ~= nM2 ifTrue:[
+ nM1 > nM2 ifTrue:[
+ m2 := m2 bitShift:(nM1-nM2).
+ ] ifFalse:[
+ m1 := m1 bitShift:(nM2-nM1).
+ ].
+ ].
+
+ e1 := anIEEEFloat exponent.
+ e2 := self exponent.
+ e1 = e2 ifTrue:[^ m1 < m2].
+ ^ e1 < e2.
!
productFromIEEEFloat:anIEEEFloat
@@ -648,61 +698,13 @@
!IEEEFloat methodsFor:'printing & storing'!
-displayOn:aStream
-aStream nextPutAll:'IEEEFloat'.
-^ self.
-!
-
-displayString
-^ 'IEEEFloat'.
-
-!
-
printOn:aStream
-aStream nextPutAll:'IEEEFloat'.
-^ self.
thisContext isRecursive ifTrue:[
aStream nextPutAll:'IEEEFloat (error while printing)'.
^ self.
].
-
+ "/ super printOn:aStream.
PrintfScanf printf:'%g' on:aStream argument:self.
-!
-
-printOn:aStream base:b
-aStream nextPutAll:'IEEEFloat'.
-!
-
-printOn:aStream base:b showRadix:showRadix
-aStream nextPutAll:'IEEEFloat'.
-!
-
-printOn:aStream thousandsSeparator:thousandsSeparator
-aStream nextPutAll:'IEEEFloat'.
-!
-
-printString
-^ 'IEEEFloat'.
-!
-
-printStringFormat:formatString
-^ 'IEEEFloat'.
-!
-
-printStringRadix:base
-^ 'IEEEFloat'.
-!
-
-printStringRadix:base showRadix:showRadixBoolean
-^ 'IEEEFloat'.
-!
-
-printStringScientific
-^ 'IEEEFloat'.
-!
-
-printfPrintString:formatString
-^ 'IEEEFloat'.
! !
!IEEEFloat methodsFor:'queries'!
@@ -748,6 +750,12 @@
"
^ (self basicSize * 8) - exponentSize - 1 "/ sign
+!
+
+radix
+ "Answer the exponent's radix"
+
+ ^ 2
! !
!IEEEFloat methodsFor:'testing'!