19 lines
549 B
Text
19 lines
549 B
Text
x@(Number traits) raisedTo: y@(Integer traits)
|
|
[
|
|
y isZero ifTrue: [^ x unit].
|
|
x isZero \/ [y = 1] ifTrue: [^ x].
|
|
y isPositive
|
|
ifTrue:
|
|
"(x * x raisedTo: y // 2) * (x raisedTo: y \\ 2)"
|
|
[| count result |
|
|
count: 1.
|
|
[(count: (count bitShift: 1)) < y] whileTrue.
|
|
result: x unit.
|
|
[count isPositive]
|
|
whileTrue:
|
|
[result: result squared.
|
|
(y bitAnd: count) isZero ifFalse: [result: result * x].
|
|
count: (count bitShift: -1)].
|
|
result]
|
|
ifFalse: [(x raisedTo: y negated) reciprocal]
|
|
].
|