An even better definition for (^) (trac #1687)
authorIan Lynagh <igloo@earth.li>
Thu, 20 Mar 2008 00:39:57 +0000 (00:39 +0000)
committerIan Lynagh <igloo@earth.li>
Thu, 20 Mar 2008 00:39:57 +0000 (00:39 +0000)
GHC/Real.lhs

index 17faed9..2994f1e 100644 (file)
@@ -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 (^^) ::