#UI_ENHANCEMENT by cg
class: Number
added:
#printfPrintString:
#sqrt_withAccuracy:fromInitialGuess:
changed: #sqrt_withAccuracy:
--- a/Number.st Thu Jun 22 13:57:19 2017 +0200
+++ b/Number.st Thu Jun 22 14:33:41 2017 +0200
@@ -1917,6 +1917,21 @@
"
!
+printfPrintString:formatString
+ "Return a printed representation of the receiver as specified by formatString,
+ which is defined by printf."
+
+ ^ PrintfScanf printf:formatString argument:self
+
+ "
+ 2.0 asQDouble printfPrintString:'%10f'
+ 2.0 asQDouble printfPrintString:'%10.8f'
+ 2.0 printfPrintString:'%10.8f'
+ "
+
+ "Created: / 22-06-2017 / 13:55:22 / cg"
+!
+
radixPrintStringRadix:radix
"return a string representation of the receiver in the specified
base; prepend XXr to the string"
@@ -2407,38 +2422,84 @@
!
sqrt_withAccuracy:epsilon
- "compute square root of the receiver using newtom/heron algorithm"
+ "compute square root of the receiver using newton-raphson/heron algorithm"
"
Use the Heron algorithm:
- x_n + (a / x_n)
- x_n+1 = ---------------
- 2
-
- sqrt(a) = x_n
+ x_n + (a / x_n)
+ x_n+1 = ---------------
+ 2
+
+ sqrt(a) = x_n
+ "
+
+ ^ self sqrt_withAccuracy:epsilon fromInitialGuess:(self / 2)
+
+ "
+ 2 sqrt 1.4142135623731 - computed by CPU/FPU
+
+ 2q sqrt 1.414213562373095049 - computed by CPU/FPU
+
+ 2q sqrt_withAccuracy:0.01 1.414215686274509804 - computed by Smalltalk
+ 2q sqrt_withAccuracy:0.0001 1.414213562374689911
+ 2q sqrt_withAccuracy:0.0000001 1.414213562373095049
+ 2q sqrt_withAccuracy:0.0000000001 1.414213562373095049
+ 2q sqrt_withAccuracy:0.000000000001 1.414213562373095049
+
+ 2q sqrt_withAccuracy:LongFloat epsilon 1.414213562373095049
+
+ (4 sqrt_withAccuracy:Integer epsilon) asFloat
+
+ MessageTally spyOn:[ |arg|
+ arg := 2 asLongFloat.
+ 1000000 timesRepeat:[
+ arg sqrt_withAccuracy:0.000000000001
+ ]
+ ]
+ Time millisecondsToRun:[ |arg|
+ arg := 2 asLongFloat.
+ 1000000 timesRepeat:[
+ arg sqrt_withAccuracy:0.000000000001
+ ]
+ ]
+ "
+
+ "Modified (comment): / 22-06-2017 / 14:01:16 / cg"
+!
+
+sqrt_withAccuracy:epsilon fromInitialGuess:guess
+ "compute square root of the receiver using newton-raphson/heron algorithm"
+ "
+ Use the Heron algorithm:
+
+ x_n + (a / x_n)
+ x_n+1 = ---------------
+ 2
+
+ sqrt(a) = x_n
"
|approx|
self <= 0 ifTrue:[
- self = 0 ifTrue:[
- ^ self
- ].
- ^ self class
- raise:#imaginaryResultSignal
- receiver:self
- selector:#sqrt
- arguments:#()
- errorString:'bad (negative) receiver in sqrt'
+ self = 0 ifTrue:[
+ ^ self
+ ].
+ ^ self class
+ raise:#imaginaryResultSignal
+ receiver:self
+ selector:#sqrt
+ arguments:#()
+ errorString:'bad (negative) receiver in sqrt'
].
- approx := 1.
+ approx := guess.
[
- |lastApprox|
-
- lastApprox := approx.
- approx := ((self / approx) + approx) / 2.
- (approx - lastApprox) abs > epsilon
+ |lastApprox|
+
+ lastApprox := approx.
+ approx := ((self / approx) + approx) / 2.
+ (approx - lastApprox) abs > epsilon
] whileTrue.
^ approx
@@ -2455,18 +2516,20 @@
(4 sqrt_withAccuracy:Integer epsilon) asFloat
MessageTally spyOn:[ |arg|
- arg := 2 asLongFloat.
- 1000000 timesRepeat:[
- arg sqrt_withAccuracy:0.000000000001
- ]
+ arg := 2 asLongFloat.
+ 1000000 timesRepeat:[
+ arg sqrt_withAccuracy:0.000000000001
+ ]
]
Time millisecondsToRun:[ |arg|
- arg := 2 asLongFloat.
- 1000000 timesRepeat:[
- arg sqrt_withAccuracy:0.000000000001
- ]
+ arg := 2 asLongFloat.
+ 1000000 timesRepeat:[
+ arg sqrt_withAccuracy:0.000000000001
+ ]
]
"
+
+ "Created: / 22-06-2017 / 13:59:48 / cg"
!
tan_withAccuracy:epsilon