RosettaCodeData/Task/Exponentiation-operator/Slate/exponentiation-operator-1.slate
Ingy döt Net d066446780 langs a-z
2013-04-10 22:43:41 -07:00

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]
].