[squeakdev] The Trunk: Kernelnice.646.mcz
commits at source.squeak.org
commits at source.squeak.org
Sat Oct 29 13:26:31 UTC 2011
Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernelnice.646.mcz
==================== Summary ====================
Name: Kernelnice.646
Author: nice
Time: 29 October 2011, 3:25:43.994 pm
UUID: cd46101c40a449fdb358a8d0c555810f
Ancestors: Kernelnice.645
There might be a best inexact square root approximation than Float infinity to some LargePositiveInteger in the range (1<<541<<970 to: 1<<541<<1994)
=============== Diff against Kernelnice.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!
More information about the Squeakdev
mailing list
