[squeak-dev] The Trunk: Kernel-nice.640.mcz

commits at source.squeak.org commits at source.squeak.org
Mon Oct 17 21:03:21 UTC 2011


Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.640.mcz

==================== Summary ====================

Name: Kernel-nice.640
Author: nice
Time: 17 October 2011, 11:02:36.274 pm
UUID: 34b1c868-c862-4f76-ab25-28e24f840384
Ancestors: Kernel-nice.639

Add Integer>>#nthRootTruncated:
Simplify Integer>>#sqrtFloor

A pity this last one was not named sqrtTruncated for consistency...
(I think I saw sqrtTruncated in st80 v2.5, or was it in Smalltalk V ?)

=============== Diff against Kernel-nice.639 ===============

Item was added:
+ ----- Method: Integer>>nthRootTruncated: (in category 'mathematical functions') -----
+ nthRootTruncated: aPositiveInteger
+ 	"Answer the integer part of the nth root of the receiver."
+ 	| guess guessToTheNthMinusOne delta |
+ 	self = 0 ifTrue: [^0].
+ 	self negative
+ 		ifTrue:
+ 			[aPositiveInteger even ifTrue: [ ArithmeticError signal: 'Negative numbers don''t have even roots.' ].
+ 			^(self negated nthRootFloor: aPositiveInteger) negated].
+ 	guess := 1 bitShift: self highBitOfMagnitude + aPositiveInteger - 1 // aPositiveInteger.
+ 	[
+ 		guessToTheNthMinusOne := guess raisedTo: aPositiveInteger - 1.
+ 		delta := (guess * guessToTheNthMinusOne - self) // (guessToTheNthMinusOne * aPositiveInteger).
+ 		delta = 0 ] whileFalse:
+ 			[ guess := guess - delta ].
+ 	( (guess := guess - 1) raisedTo: aPositiveInteger) > self  ifTrue:
+ 			[ guess := guess - 1 ].
+ 	^guess!

Item was changed:
  ----- Method: Integer>>sqrtFloor (in category 'mathematical functions') -----
  sqrtFloor
  	"Return the integer part of the square root of self"
  
+ 	| guess delta |
- 	| guess guessSquared delta |
  	guess := 1 bitShift: self highBit + 1 // 2.
  	[
+ 		delta := guess squared - self // (guess bitShift: 1).
- 		guessSquared := guess * guess.
- 		delta := guessSquared - self // (guess bitShift: 1).
  		delta = 0 ] whileFalse: [
  			guess := guess - delta ].
+ 	^guess - 1!
- 	guessSquared = self ifFalse: [ guess := guess - 1 ].
- 	^guess!




More information about the Squeak-dev mailing list