Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.646.mcz
==================== Summary ====================
Name: Kernel-nice.646
Author: nice
Time: 29 October 2011, 3:25:43.994 pm
UUID: cd46101c-40a4-49fd-b358-a8d0c555810f
Ancestors: Kernel-nice.645
There might be a best inexact square root approximation than Float infinity to some LargePositiveInteger in the range (1<<54-1<<970 to: 1<<54-1<<1994)
=============== Diff against Kernel-nice.645 ===============
Item was changed:
----- Method: Integer>>sqrt (in category 'mathematical functions') -----
sqrt
"Answer the square root of the receiver."
| selfAsFloat floatResult guess |
selfAsFloat := self asFloat.
floatResult := selfAsFloat sqrt.
floatResult isInfinite ifFalse: [
guess := floatResult truncated.
"If got an exact answer, answer it. Otherwise answer float approximate answer."
guess squared = self
ifTrue: [ ^ guess ]].
"In this case, maybe it failed because we are such a big integer that the Float method becomes
inexact, even if we are a whole square number. So, try the slower but more general method"
selfAsFloat >= Float maxExactInteger asFloat squared
ifTrue: [
guess := self sqrtFloor.
guess squared = self ifTrue: [
+ ^guess ].
+
+ "Nothing else can be done. No exact answer means answer must be a Float.
+ Answer the best we have which is the rounded sqrt."
+ guess := (self * 4) sqrtFloor.
+ ^(guess // 2 + (guess \\ 2)) asFloat].
- ^guess ]].
"We need an approximate result"
^floatResult!
Item was changed:
----- Method: LargePositiveInteger>>sqrt (in category 'mathematical functions') -----
sqrt
"If we know for sure no exact solution exists, then just answer the cheap float approximation without wasting time."
+ | selfAsFloat |
+ self mightBeASquare
+ ifFalse:
+ [selfAsFloat := self asFloat.
+ selfAsFloat isFinite ifTrue: [^self asFloat sqrt ]].
- self mightBeASquare ifFalse: [
- ^self asFloat sqrt ].
+ "If some exact solution might exist, or self asFloat isInfinite, call potentially expensive super"
- "If some exact solution might exist, call potentially expensive super"
^super sqrt!

Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.646.mcz
==================== Summary ====================
Name: Kernel-nice.646
Author: nice
Time: 29 October 2011, 3:25:43.994 pm
UUID: cd46101c-40a4-49fd-b358-a8d0c555810f
Ancestors: Kernel-nice.645
There might be a best inexact square root approximation than Float infinity to some LargePositiveInteger in the range (1<<54-1<<970 to: 1<<54-1<<1994)
=============== Diff against Kernel-nice.645 ===============
Item was changed:
----- Method: Integer>>sqrt (in category 'mathematical functions') -----
sqrt
"Answer the square root of the receiver."
| selfAsFloat floatResult guess |
selfAsFloat := self asFloat.
floatResult := selfAsFloat sqrt.
floatResult isInfinite ifFalse: [
guess := floatResult truncated.
"If got an exact answer, answer it. Otherwise answer float approximate answer."
guess squared = self
ifTrue: [ ^ guess ]].
"In this case, maybe it failed because we are such a big integer that the Float method becomes
inexact, even if we are a whole square number. So, try the slower but more general method"
selfAsFloat >= Float maxExactInteger asFloat squared
ifTrue: [
guess := self sqrtFloor.
guess squared = self ifTrue: [
+ ^guess ].
+
+ "Nothing else can be done. No exact answer means answer must be a Float.
+ Answer the best we have which is the rounded sqrt."
+ guess := (self * 4) sqrtFloor.
+ ^(guess // 2 + (guess \\ 2)) asFloat].
- ^guess ]].
"We need an approximate result"
^floatResult!
Item was changed:
----- Method: LargePositiveInteger>>sqrt (in category 'mathematical functions') -----
sqrt
"If we know for sure no exact solution exists, then just answer the cheap float approximation without wasting time."
+ | selfAsFloat |
+ self mightBeASquare
+ ifFalse:
+ [selfAsFloat := self asFloat.
+ selfAsFloat isFinite ifTrue: [^self asFloat sqrt ]].
- self mightBeASquare ifFalse: [
- ^self asFloat sqrt ].
+ "If some exact solution might exist, or self asFloat isInfinite, call potentially expensive super"
- "If some exact solution might exist, call potentially expensive super"
^super sqrt!

Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.646.mcz
==================== Summary ====================
Name: Kernel-nice.646
Author: nice
Time: 29 October 2011, 3:25:43.994 pm
UUID: cd46101c-40a4-49fd-b358-a8d0c555810f
Ancestors: Kernel-nice.645
There might be a best inexact square root approximation than Float infinity to some LargePositiveInteger in the range (1<<54-1<<970 to: 1<<54-1<<1994)
=============== Diff against Kernel-nice.645 ===============
Item was changed:
----- Method: Integer>>sqrt (in category 'mathematical functions') -----
sqrt
"Answer the square root of the receiver."
| selfAsFloat floatResult guess |
selfAsFloat := self asFloat.
floatResult := selfAsFloat sqrt.
floatResult isInfinite ifFalse: [
guess := floatResult truncated.
"If got an exact answer, answer it. Otherwise answer float approximate answer."
guess squared = self
ifTrue: [ ^ guess ]].
"In this case, maybe it failed because we are such a big integer that the Float method becomes
inexact, even if we are a whole square number. So, try the slower but more general method"
selfAsFloat >= Float maxExactInteger asFloat squared
ifTrue: [
guess := self sqrtFloor.
guess squared = self ifTrue: [
+ ^guess ].
+
+ "Nothing else can be done. No exact answer means answer must be a Float.
+ Answer the best we have which is the rounded sqrt."
+ guess := (self * 4) sqrtFloor.
+ ^(guess // 2 + (guess \\ 2)) asFloat].
- ^guess ]].
"We need an approximate result"
^floatResult!
Item was changed:
----- Method: LargePositiveInteger>>sqrt (in category 'mathematical functions') -----
sqrt
"If we know for sure no exact solution exists, then just answer the cheap float approximation without wasting time."
+ | selfAsFloat |
+ self mightBeASquare
+ ifFalse:
+ [selfAsFloat := self asFloat.
+ selfAsFloat isFinite ifTrue: [^self asFloat sqrt ]].
- self mightBeASquare ifFalse: [
- ^self asFloat sqrt ].
+ "If some exact solution might exist, or self asFloat isInfinite, call potentially expensive super"
- "If some exact solution might exist, call potentially expensive super"
^super sqrt!

Nicolas Cellier uploaded a new version of KernelTests to project The Trunk:
http://source.squeak.org/trunk/KernelTests-nice.208.mcz
==================== Summary ====================
Name: KernelTests-nice.208
Author: nice
Time: 29 October 2011, 3:24:06.652 pm
UUID: 98713787-6ba4-424d-b622-0c1f459ac49c
Ancestors: KernelTests-nice.207
There might be a best inexact square root approximation than infinity to some LargePositiveInteger in the range (1<<54-1<<970 to: 1<<54-1<<1994)
=============== Diff against KernelTests-nice.207 ===============
Item was added:
+ ----- Method: IntegerTest>>testBigReceiverInexactSqrt (in category 'tests - mathematical functions') -----
+ testBigReceiverInexactSqrt
+ "
+ IntegerTest new testBigReceiverInexactSqrt
+ "
+
+ "Inexact 3rd root (not a whole cube number), so a Float must be answered.
+ However, receiver is too big for Float arithmethic."
+ | bigNum result |
+ bigNum := 100 factorial squared + 1. "Add 1 so it is not a whole square"
+ self assert: bigNum asFloat isInfinite. "Otherwise, we chose a bad sample"
+ result := bigNum sqrt.
+ self assert: result class == Float.
+ self deny: result isInfinite.
+ self assert: result = 100 factorial asFloat. "No other float is closer. See following lines"
+ self assert: (result successor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.
+ self assert: (result predecessor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.!

Nicolas Cellier uploaded a new version of KernelTests to project The Trunk:
http://source.squeak.org/trunk/KernelTests-nice.208.mcz
==================== Summary ====================
Name: KernelTests-nice.208
Author: nice
Time: 29 October 2011, 3:24:06.652 pm
UUID: 98713787-6ba4-424d-b622-0c1f459ac49c
Ancestors: KernelTests-nice.207
There might be a best inexact square root approximation than infinity to some LargePositiveInteger in the range (1<<54-1<<970 to: 1<<54-1<<1994)
=============== Diff against KernelTests-nice.207 ===============
Item was added:
+ ----- Method: IntegerTest>>testBigReceiverInexactSqrt (in category 'tests - mathematical functions') -----
+ testBigReceiverInexactSqrt
+ "
+ IntegerTest new testBigReceiverInexactSqrt
+ "
+
+ "Inexact 3rd root (not a whole cube number), so a Float must be answered.
+ However, receiver is too big for Float arithmethic."
+ | bigNum result |
+ bigNum := 100 factorial squared + 1. "Add 1 so it is not a whole square"
+ self assert: bigNum asFloat isInfinite. "Otherwise, we chose a bad sample"
+ result := bigNum sqrt.
+ self assert: result class == Float.
+ self deny: result isInfinite.
+ self assert: result = 100 factorial asFloat. "No other float is closer. See following lines"
+ self assert: (result successor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.
+ self assert: (result predecessor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.!

Nicolas Cellier uploaded a new version of KernelTests to project The Trunk:
http://source.squeak.org/trunk/KernelTests-nice.208.mcz
==================== Summary ====================
Name: KernelTests-nice.208
Author: nice
Time: 29 October 2011, 3:24:06.652 pm
UUID: 98713787-6ba4-424d-b622-0c1f459ac49c
Ancestors: KernelTests-nice.207
There might be a best inexact square root approximation than infinity to some LargePositiveInteger in the range (1<<54-1<<970 to: 1<<54-1<<1994)
=============== Diff against KernelTests-nice.207 ===============
Item was added:
+ ----- Method: IntegerTest>>testBigReceiverInexactSqrt (in category 'tests - mathematical functions') -----
+ testBigReceiverInexactSqrt
+ "
+ IntegerTest new testBigReceiverInexactSqrt
+ "
+
+ "Inexact 3rd root (not a whole cube number), so a Float must be answered.
+ However, receiver is too big for Float arithmethic."
+ | bigNum result |
+ bigNum := 100 factorial squared + 1. "Add 1 so it is not a whole square"
+ self assert: bigNum asFloat isInfinite. "Otherwise, we chose a bad sample"
+ result := bigNum sqrt.
+ self assert: result class == Float.
+ self deny: result isInfinite.
+ self assert: result = 100 factorial asFloat. "No other float is closer. See following lines"
+ self assert: (result successor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.
+ self assert: (result predecessor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.!

Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.645.mcz
==================== Summary ====================
Name: Kernel-nice.645
Author: nice
Time: 29 October 2011, 11:42:54.825 am
UUID: 6c7fe149-d362-481d-803f-eecbec00619c
Ancestors: Kernel-ael.643
Yet another version of #nthRoot: from Cuis
1) Receivers too big for Float arithmetic but without exact answer, and a better float than infinity is possible.
2) Besides, a small performance tweak: don't call 'selfAsFloat nthRoot: aPositiveInteger' if the result will be infinity, i.e. if the receiver is infinity.
=============== Diff against Kernel-ael.643 ===============
Item was changed:
----- Method: Integer>>nthRoot: (in category 'mathematical functions') -----
nthRoot: aPositiveInteger
+ "Answer the nth root of the receiver.
+ See #nthRootAlt: for an alternative implementation."
- "Answer the nth root of the receiver."
+ | selfAsFloat floatResult guess delta higher lower raised |
- | selfAsFloat floatResult guess raised higher lower delta |
selfAsFloat := self asFloat.
- floatResult := selfAsFloat nthRoot: aPositiveInteger.
+ "If we can't do Float arithmetic because we are too big, then look for an exact answer in exact arithmetic"
+ selfAsFloat isInfinite ifTrue: [
- "If we can't do Float arithmetic, then look for an exact answer in exact arithmetic"
- floatResult isInfinite ifTrue: [
guess := self nthRootTruncated: aPositiveInteger.
+ (guess raisedToInteger: aPositiveInteger) = self
+ ifTrue: [ ^ guess ].
+ "Nothing else can be done. No exact answer means answer must be a Float.
+ Answer the best we have."
+ ^guess asFloat ].
- (guess raisedToInteger: aPositiveInteger) = self ifTrue: [^guess].
- ^floatResult ].
+ floatResult := selfAsFloat nthRoot: aPositiveInteger.
guess := floatResult rounded.
"If got an exact answer, answer it."
raised := guess raisedToInteger: aPositiveInteger.
raised = self
ifTrue: [ ^ guess ].
"In this case, maybe it failed because we are such a big integer that the Float
method gets inexact, even if we are a whole square number.
+ Note 1(jmv): This algorithm is faster than #nthRootTruncated: for big n (aPositiveInteger)
+ but fails if self asFloat isInfinite.
+ Note 2(jmv): The algorithms I found for computing the nthRoot would havily use
- Note (jmv): The algorithms I found for computing the nthRoot would havily use
very large fractions. I wrote this one, that doesn't create fractions."
selfAsFloat abs >= (Float maxExactInteger asFloat raisedToInteger: aPositiveInteger)
ifTrue: [
raised > self
ifTrue: [
higher := guess.
delta := floatResult predecessor - floatResult.
[
floatResult := floatResult + delta.
lower := floatResult rounded.
(lower raisedToInteger: aPositiveInteger) > self ] whileTrue: [
delta := delta * 2.
higher := lower ] ]
ifFalse: [
lower := guess.
delta := floatResult successor - floatResult.
[
floatResult := floatResult + delta.
higher := floatResult rounded.
(higher raisedToInteger: aPositiveInteger) < self ] whileTrue: [
delta := delta * 2.
lower := higher ]].
[ higher - lower > 1 ] whileTrue: [
guess := lower + higher // 2.
raised := guess raisedToInteger: aPositiveInteger.
raised = self
ifTrue: [
^ guess ].
raised > self
ifTrue: [ higher := guess ]
ifFalse: [ lower := guess ]]].
"We need an approximate result"
^floatResult!