RosettaCodeData/Task/Exponentiation-operator/Ela/exponentiation-operator-2.ela
2016-12-05 22:15:40 +01:00

20 lines
605 B
Text

open number
//Function quot from number module is defined only for
//integral numbers. We can use this as an universal quot.
uquot x y | x is Integral = x `quot` y
| else = x / y
//Changing implementation by using generic numeric literals
//(e.g. 2u) and elimitating all comparisons with 0.
!x ^ n | n ~= 0u = 1u
| n > 0u = f x (n - 1u) x
| else = fail "Negative exponent"
where f a d y
| d ~= 0u = y
| else = g a d
where g b i | even i = g (b * b) (i `uquot` 2u)
| else = f b (i - 1u) (b * y)
(12 ^ 4, 12.34 ^ 4.04)