#REFACTORING by cg
class: LargeFloat
added: #precision:
comment/format in:
#mantissa:exponent:
#mantissa:exponent:precision:
#printOn:
#productFromLargeFloat:
changed: #lessFromLargeFloat:
class: LargeFloat class
comment/format in: #documentation
--- a/LargeFloat.st Mon Jul 17 14:20:53 2017 +0200
+++ b/LargeFloat.st Mon Jul 17 14:51:09 2017 +0200
@@ -44,7 +44,8 @@
This class provides arbitrary precision floats. These are represented as:
exponent,
mantissa
-
+ precision
+
[author:]
Claus Gittinger
@@ -497,18 +498,48 @@
!
lessFromLargeFloat:aLargeFloat
+ "return true if aLargeFloat < self"
+
|otherExponent otherMantissa|
otherExponent := aLargeFloat exponent.
otherMantissa := aLargeFloat mantissa.
- otherExponent > exponent ifTrue:[
- ^ otherMantissa < (mantissa bitShift:(otherExponent-exponent))
+ exponent < otherExponent ifTrue:[
+ "/ my exponent is < than the other number's exponent.
+ "/ left-shift the other mantissa
+ ^ (otherMantissa bitShift:(otherExponent-exponent)) < mantissa
].
otherExponent < exponent ifTrue:[
- ^ (otherMantissa bitShift:(exponent-otherExponent)) < mantissa
+ "/ my exponent is > than the other number's exponent.
+ "/ left-shift my mantissa
+ ^ otherMantissa < (mantissa bitShift:(exponent-otherExponent))
].
+
+ "/ same exponents
^ otherMantissa < mantissa
+
+ "
+ 4.0 asLargeFloat < 4.5 asLargeFloat
+ 4.1 asLargeFloat < 4.2 asLargeFloat
+ 4.1 asLargeFloat < 4.0 asLargeFloat
+
+ 4.0 asLargeFloat < 8.0 asLargeFloat
+
+ 1.0 asLargeFloat < 2.0 asLargeFloat
+ 2.0 asLargeFloat < 1.0 asLargeFloat
+ 3.0 asLargeFloat < 1.0 asLargeFloat
+ 3.0 asLargeFloat < 2.0 asLargeFloat
+
+ 5.0 asLargeFloat < 500.0 asLargeFloat
+ 499.0 asLargeFloat < 500.0 asLargeFloat
+
+ 5.0 asLargeFloat < 7.0 asLargeFloat
+ 5.0 asLargeFloat < 17.0 asLargeFloat
+
+ "
+
+ "Modified (comment): / 17-07-2017 / 14:47:14 / cg"
!
productFromLargeFloat:aLargeFloat
@@ -537,6 +568,13 @@
mantissa:(mantissa * otherMantissa)
exponent:(exponent + otherExponent)
precision:(self precision min:aLargeFloat precision)
+
+ "
+ 5.0 asLargeFloat * 4
+ (5.0 asLargeFloat precision:20) * 4
+ "
+
+ "Modified (comment): / 17-07-2017 / 14:50:42 / cg"
!
quotientFromLargeFloat:aLargeFloat
@@ -626,39 +664,40 @@
aStream nextPutAll:'Invalid'. ^ self.
].
- exponent > 0 ifTrue:[
-self halt.
+ exponent >= 0 ifTrue:[
(mantissa bitShift:exponent) printOn:aStream.
aStream nextPutAll:'.0'.
^ self
].
((mantissa / (1 bitShift:exponent negated)) asFixedPoint:6) printOn:aStream.
- "
-
- "
+ "Modified (comment): / 17-07-2017 / 14:48:34 / cg"
! !
!LargeFloat methodsFor:'private'!
mantissa:mantissaArg exponent:exponentArg
"set instance variables.
- Notice, that the floats value is m * 2^e"
+ Notice, that the float's value is m * 2^e"
exponent := exponentArg.
mantissa := mantissaArg.
precision := Infinity positive.
self normalize.
+
+ "Modified (comment): / 17-07-2017 / 14:50:14 / cg"
!
mantissa:mantissaArg exponent:exponentArg precision:precisionArg
"set instance variables.
- Notice, that the floats value is m * 2^e"
+ Notice, that the float's value is m * 2^e"
exponent := exponentArg.
mantissa := mantissaArg.
precision := precisionArg.
self normalize
+
+ "Modified (comment): / 17-07-2017 / 14:50:10 / cg"
!
normalize
@@ -683,6 +722,16 @@
self mantissa:10 exponent:0
self mantissa:10 exponent:1
"
+!
+
+precision:precisionArg
+ "set instance variables.
+ Notice, that the float's value is m * 2^e"
+
+ precision := precisionArg.
+ self normalize
+
+ "Created: / 17-07-2017 / 14:50:04 / cg"
! !
!LargeFloat methodsFor:'queries'!