[squeakdev] The Trunk: Kernelnice.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/Kernelnice.640.mcz
==================== Summary ====================
Name: Kernelnice.640
Author: nice
Time: 17 October 2011, 11:02:36.274 pm
UUID: 34b1c868c8624f76ab2528e24f840384
Ancestors: Kernelnice.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 Kernelnice.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 Squeakdev
mailing list
