#BUGFIX by exept
authorClaus Gittinger <cg@exept.de>
Wed, 27 Nov 2019 11:16:07 +0100
changeset 5306 819725b85a08
parent 5305 e45c3064978f
child 5307 e9b56e858f44
#BUGFIX by exept class: QDouble added: #asLargeFloat #asLongFloat #sign removed: #truncated comment/format in: #asQDouble #truncatedAsFloat changed: #asFloat #exp_withAccuracy: #ln
QDouble.st
--- a/QDouble.st	Tue Nov 26 04:27:50 2019 +0000
+++ b/QDouble.st	Wed Nov 27 11:16:07 2019 +0100
@@ -1358,11 +1358,15 @@
 !
 
 asFloat
-    ^ self d0
+    ^ self d0 + self d1
 
     "
-     (QDouble fromFloat:1.0) asFloat
-     (QDouble fromFloat:2.0) asFloat
+     (QDouble fromFloat:1.0) asFloat  -> 1.0
+     (QDouble fromFloat:2.0) asFloat  -> 2.0
+     (2.0 asQDouble + 1e-14) asFloat  -> 2.00000000000001
+     (2.0 + 1e-14) - 2.0              -> 1.02140518265514E-14
+     (2.0 + 1e-15) - 2.0              -> 8.88178419700125E-16
+     (2.0 + 1e-16) - 2.0              -> 0.0
     "
 
     "Created: / 12-06-2017 / 18:15:27 / cg"
@@ -1378,12 +1382,47 @@
     "Created: / 19-06-2017 / 18:07:17 / cg"
 !
 
+asLargeFloat
+    ^ (self d0 asLargeFloat precision:self precision) + self d1 + self d2 + self d3
+
+    "
+     (QDouble fromFloat:1.0) asLargeFloat    -> 1.000000000000000000000000000000
+     (QDouble fromFloat:2.0) asLargeFloat    -> 2.000000000000000000000000000000
+     (2.0 asQDouble + 1e-14) asLargeFloat    -> 2.000000000000010214051826551440
+     (2.0 asLargeFloat + 1e-14) - 2.0        -> 0.000000000000010214051826551440
+     (2.0  + 1e-14) - 2.0                   -> 1.02140518265514E-14
+     (2.0 asLargeFloat + 1e-14) - 2.0       -> 0.000000000000010214051826551440
+     (2.0 asLargeFloat + 1e-15) - 2.0       -> 0.000000000000000888178419700125
+     (2.0 asLargeFloat + 1e-16) - 2.0       -> 0.0
+     (2QL + 1QL-14) - 2QL                   -> 0.000000000000010000000000000000
+    "
+!
+
+asLongFloat
+    ^ self d0 asLongFloat + self d1
+
+    "
+     (QDouble fromFloat:1.0) asLongFloat    -> 1.0
+     (QDouble fromFloat:2.0) asLongFloat    -> 2.0
+     (2.0 asQDouble + 1e-14) asLongFloat    -> 2.00000000000001
+     (2.0 asLongFloat + 1e-14) - 2.0        -> 1.00000303177028016E-14
+     (2.0  + 1e-14) - 2.0                   -> 1.02140518265514E-14
+     (2.0 asLargeFloat + 1e-14) - 2.0       -> 0.000000000000010214051826551440
+     (2.0 asLargeFloat + 1e-15) - 2.0       -> 0.000000000000000888178419700125
+     (2.0 asLargeFloat + 1e-16) - 2.0       -> 0.0
+     (2QL + 1QL-14) - 2QL                   -> 0.000000000000010000000000000000
+    "
+
+    "Created: / 12-06-2017 / 18:15:27 / cg"
+    "Modified: / 13-06-2017 / 17:56:50 / cg"
+!
+
 asQDouble
     "return a QDouble with same value as myself."
 
     ^ self
 
-    "Created: / 15-06-2017 / 12:08:02 / cg"
+
 !
 
 asTrueFraction
@@ -2487,44 +2526,44 @@
     "/  e^x = 1 + --- + --- + --- ...
     "/             1!!    2!!    3!!
 
-    |x2 facN num den approx delta|
-
-    x2 := self asFloat squared.
+    |x2 n num den approx delta|
+
+    x2 := self asLimitedPrecisionReal squared.
 
     num := x2.
-    den := 2.
-    facN := 2.
+    den := self coerce:2.
+    n := 3.
     approx := self + 1 + (num / den).
 
     [
-	facN := facN + 1.
-	den := den * facN.
-	num := num * self.
-
-	delta := num / den.
-	"/ Transcript showCR:delta.
-	delta isNaN ifTrue:[self halt:'nan when dividing for delta'. num / den].
-	delta = 0 ifTrue:[self halt:'zero delta'].
-	approx := approx + delta.
+        n := n + 1.
+        den := den * n.
+        num := num * self.
+
+        delta := num / den.
+        "/ Transcript showCR:delta.
+        delta isNaN ifTrue:[self halt:'nan when dividing for delta'. num / den].
+        delta = 0 ifTrue:[self halt:'zero delta'].
+        approx := approx + delta.
     ] doUntil:[delta abs <= epsilon].
 
     ^ approx
 
     "
      wolfram:
-							7.389056098930650227230427460575007813180315570551847324087
+                                                        7.389056098930650227230427460575007813180315570551847324087
 
      2 asQDouble exp_withAccuracy:QDouble epsilon       7.38905609893065022723
      2 asQDouble exp_withAccuracy:LongFloat epsilon     7.38905609893065022723
      2 asQDouble exp_withAccuracy:Float epsilon         7.38905609893065022489
 
-							2.718281828459045235360287471352662497757247093699959574966...
+                                                        2.718281828459045235360287471352662497757247093699959574966...
 
      1 asQDouble exp_withAccuracy:QDouble epsilon       2.71828182845904523536
      1 asQDouble exp_withAccuracy:LongFloat epsilon     2.71828182845904523536
      1 asQDouble exp_withAccuracy:Float epsilon         2.71828182845904522671
 
-							1.7392749415205010473946813036112352261479840577250084... × 10^18
+                                                        1.7392749415205010473946813036112352261479840577250084... × 10^18
 
      42 asQDouble exp_withAccuracy:QDouble epsilon      NAN
      42 asQDouble exp_withAccuracy:LongFloat epsilon    1.73927494152050104739e18
@@ -2569,21 +2608,21 @@
 
     |d0 x|
 
-    ^ super ln.
+    "/ ^ super ln.
 
     d0 := self d0.
     d0 = 1.0 ifTrue:[
-	self isOne ifTrue:[ ^ self class zero ].
+        self isOne ifTrue:[ ^ self class zero ].
     ].
     d0 > 0.0 ifTrue:[
-	"/ initial approx.
-	x := d0 ln asQDouble.
-	"/ three more iterations of newton...
-	x := x + (self / (x exp)) - 1.0.
-	x := x + (self / (x exp)) - 1.0.
-	x := x + (self / (x exp)) - 1.0.
-
-	^ x
+        "/ initial approx.
+        x := d0 ln asQDouble.
+        "/ three more iterations of newton...
+        x := x + (self / (x exp)) - 1.0.
+        x := x + (self / (x exp)) - 1.0.
+        x := x + (self / (x exp)) - 1.0.
+
+        ^ x
     ].
 
     "/ now done via trapInfinity; was:
@@ -2593,11 +2632,11 @@
 
     "/ if you need -INF for a zero receiver, try Number trapInfinity:[...]
     ^ self class
-	raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal])
-	receiver:self
-	selector:#ln
-	arguments:#()
-	errorString:'bad receiver in ln (not strictly positive)'
+        raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal])
+        receiver:self
+        selector:#ln
+        arguments:#()
+        errorString:'bad receiver in ln (not strictly positive)'
 
     "
      -1 ln
@@ -2607,22 +2646,22 @@
      1.0 asQDouble ln
 
      3.0 ln printfPrintString:'%60.58lf'
-	    -> 1.0986122886681097821082175869378261268138885498046875000000'
-				^
+            -> 1.0986122886681097821082175869378261268138885498046875000000'
+                                ^
 
      3.0 asQDouble ln printfPrintString:'%60.58f'
-	    -> 1.0986122886681096913952452369225257046474905578227494517347
+            -> 1.0986122886681096913952452369225257046474905578227494517347
 
      3.0 asQDouble ln printfPrintString:'%70.68f'
-	    -> 1.09861228866810969139524523692252570464749055782274945173469433364779
+            -> 1.09861228866810969139524523692252570464749055782274945173469433364779
 
      (3.0 asQDouble ln_withAccuracy:1e-64) printfPrintString:'%70.68f'
-	       1.09861228866810969139524523692252570464749055782274945173469433364475
+               1.09861228866810969139524523692252570464749055782274945173469433364475
      (3.0 asQDouble ln_withAccuracy:1e-100) printfPrintString:'%70.68f'
-	      '1.098612288668109691395245236922525704647490557822749451734694333656909'
+              '1.098612288668109691395245236922525704647490557822749451734694333656909'
 
      actual result:
-	    -> 1.0986122886681096913952452369225257046474905578227494517346943336374942932186089668736157548137320887879700290659...
+            -> 1.0986122886681096913952452369225257046474905578227494517346943336374942932186089668736157548137320887879700290659...
     "
 
     "Created: / 18-06-2017 / 23:32:54 / cg"
@@ -2745,30 +2784,6 @@
 
     "Created: / 13-06-2017 / 01:27:58 / cg"
     "Modified: / 22-06-2017 / 14:08:31 / cg"
-!
-
-truncated
-    "return the receiver truncated towards zero"
-
-    self positive ifTrue:[
-        ^ self floor
-    ].
-    ^ self ceiling
-
-    "
-     (QDouble fromFloat:4.0) truncated  
-     (QDouble fromFloat:4.1) truncated  
-     (QDouble fromFloat:0.1) truncated  
-     (QDouble fromFloat:0.0) truncated 
-
-     (QDouble fromFloat:-4.0) truncated  
-     (QDouble fromFloat:-4.1) truncated  
-     (QDouble fromFloat:-0.1) truncated  
-     (QDouble fromFloat:-0.0) truncated
-    "
-
-    "Created: / 13-06-2017 / 01:52:44 / cg"
-    "Modified (comment): / 13-06-2017 / 17:33:19 / cg"
 ! !
 
 !QDouble methodsFor:'printing & storing'!
@@ -3414,6 +3429,32 @@
     "Created: / 13-06-2017 / 01:56:53 / cg"
     "Modified: / 13-06-2017 / 17:58:41 / cg"
     "Modified (comment): / 28-05-2019 / 05:55:55 / Claus Gittinger"
+!
+
+sign
+    "return the sign of the receiver"
+
+    ^ self d0 sign
+
+    "
+     Float nan isNaN   
+     Float nan sign   
+     Float infinity sign  
+     Float infinity negated sign 
+
+     ShortFloat nan isNaN   
+     ShortFloat nan sign   
+     ShortFloat infinity sign  
+     ShortFloat infinity negated sign  
+
+     QDouble nan isNaN      
+     QDouble nan sign        
+     QDouble infinity sign  
+     QDouble infinity negated sign  
+     0 asQDouble sign  
+     1 asQDouble sign  
+     -1 asQDouble sign 
+    "
 ! !
 
 !QDouble methodsFor:'truncation & rounding'!