[squeak-dev] The Trunk: Kernel-nice.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/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!




More information about the Squeak-dev mailing list