[squeakdev] The Trunk: Kernelnice.637.mcz
commits at source.squeak.org
commits at source.squeak.org
Fri Oct 14 20:29:54 UTC 2011
Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernelnice.637.mcz
==================== Summary ====================
Name: Kernelnice.637
Author: nice
Time: 14 October 2011, 10:28:53.269 pm
UUID: 90981f8953484bf989814cc51da6bc29
Ancestors: Kernelnice.636
Implement ScaledDecimal>>nthRoot:
#mightBeASquare now verifies whether the receiver is an even power of two.
=============== Diff against Kernelnice.636 ===============
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 * guess = self ifTrue: [
^guess ]].
"We need an approximate result"
^floatResult!
Item was changed:
 Method: LargePositiveInteger>>mightBeASquare (in category 'mathematical functions') 
mightBeASquare
"In base 16, a square number can end only with 0,1,4 or 9 and
 in case 0, only 0,1,4,9 can precede it,
 in case 4, only even numbers can precede it.
See http://en.wikipedia.org/wiki/Square_number
So, in hex, the last byte must be one of:
00
10
40
90
x1
e4
x9
where x is any hex digit and e is any even digit
+ Also, the receiver must be an aven power of two."
+  lsb 
+ lsb := self digitAt: 1.
+ ^((lsb bitAnd: 7) = 1 "any1 or any9"
+ or: [(lsb bitAnd: 31) = 4 "even4"
+ or: [(lsb bitAnd: 127) = 16 "10 or 90"
+ or: [(lsb bitAnd: 191) = 0]]]) "00 or 40"
+ and: [self lowBit odd] "even power of 2"!
 "
  lsb 
 lsb := self digitAt: 1.
 ^((lsb bitAnd: 7) = 1 "any1 or any9"
 or: [(lsb bitAnd: 31) = 4 "even4"
 or: [(lsb bitAnd: 127) = 16 "10 or 90"
 or: [(lsb bitAnd: 191) = 0]]]) "00 or 40"!
Item was added:
+  Method: ScaledDecimal>>nthRoot: (in category 'mathematical functions') 
+ nthRoot: anInteger
+ "Answer the nth root of the receiver.
+ Preserve receiver class and scale if answer is exact.
+ Otherwise, answer a Float to denote inexactness."
+  nthRoot 
+ nthRoot := self asFraction nthRoot: anInteger.
+ ^nthRoot isFloat
+ ifTrue: [nthRoot]
+ ifFalse: [nthRoot asScaledDecimal: scale]!
More information about the Squeakdev
mailing list
