From 9fde0036ce00a9d66ad82de97e03a0cb5d1ddcb0 Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Thu, 20 Mar 2008 00:39:57 +0000 Subject: [PATCH] An even better definition for (^) (trac #1687) --- GHC/Real.lhs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/GHC/Real.lhs b/GHC/Real.lhs index 17faed9..2994f1e 100644 --- a/GHC/Real.lhs +++ b/GHC/Real.lhs @@ -403,13 +403,14 @@ odd = not . even Integer -> Integer -> Integer, Integer -> Int -> Integer, Int -> Int -> Int #-} -(^) :: (Num a, Integral b) => a -> b -> a -x ^ y | y < 0 = error "Negative exponent" - | y == 0 = 1 - | y == 1 = x - | odd y = x * (x ^ (y - 1)) - | otherwise = let x' = x ^ (y `div` 2) - in x' * x' +(^) :: (Num a, Integral b) => a -> b -> a +x0 ^ y0 | y0 < 0 = error "Negative exponent" + | y0 == 0 = 1 + | otherwise = f x0 y0 1 + where -- x0 ^ y0 = (x ^ y) * z + f x y z | even y = f (x * x) (y `quot` 2) z + | y == 1 = x * z + | otherwise = f (x * x) ((y - 1) `quot` 2) (x * z) -- | raise a number to an integral power {-# SPECIALISE (^^) :: -- 1.7.10.4