[commit: base] master: Adjust behaviour of gcd (f826879)
Ian Lynagh
igloo at earth.li
Sun Jun 12 17:21:44 CEST 2011
Repository : ssh://darcs.haskell.org//srv/darcs/packages/base
On branch : master
http://hackage.haskell.org/trac/ghc/changeset/f826879156a3c5eba3893cf027c11c117bcfd7ff
>---------------------------------------------------------------
commit f826879156a3c5eba3893cf027c11c117bcfd7ff
Author: Daniel Fischer <daniel.is.fischer at googlemail.com>
Date: Tue May 17 22:53:40 2011 +0200
Adjust behaviour of gcd
Change behaviour of 'gcd 0 0' from raising an exception to
returning 0 to make it total and conform to mathematical practice.
>---------------------------------------------------------------
GHC/Real.lhs | 12 +++---------
1 files changed, 3 insertions(+), 9 deletions(-)
diff --git a/GHC/Real.lhs b/GHC/Real.lhs
index 0115409..51d7db4 100644
--- a/GHC/Real.lhs
+++ b/GHC/Real.lhs
@@ -520,11 +520,10 @@ x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
in if even e then (nn :% dd) else (negate nn :% dd)
-------------------------------------------------------
--- | @'gcd' x y@ is the greatest (positive) integer that divides both @x@
+-- | @'gcd' x y@ is the greatest (nonnegative) integer that divides both @x@
-- and @y@; for example @'gcd' (-3) 6@ = @3@, @'gcd' (-3) (-6)@ = @3@,
--- @'gcd' 0 4@ = @4 at . @'gcd' 0 0@ raises a runtime error.
+-- @'gcd' 0 4@ = @4 at . @'gcd' 0 0@ = @0 at .
gcd :: (Integral a) => a -> a -> a
-gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined"
gcd x y = gcd' (abs x) (abs y)
where gcd' a 0 = a
gcd' a b = gcd' b (a `rem` b)
@@ -539,16 +538,11 @@ lcm x y = abs ((x `quot` (gcd x y)) * y)
#ifdef OPTIMISE_INTEGER_GCD_LCM
{-# RULES
"gcd/Int->Int->Int" gcd = gcdInt
-"gcd/Integer->Integer->Integer" gcd = gcdInteger'
+"gcd/Integer->Integer->Integer" gcd = gcdInteger
"lcm/Integer->Integer->Integer" lcm = lcmInteger
#-}
-gcdInteger' :: Integer -> Integer -> Integer
-gcdInteger' 0 0 = error "GHC.Real.gcdInteger': gcd 0 0 is undefined"
-gcdInteger' a b = gcdInteger a b
-
gcdInt :: Int -> Int -> Int
-gcdInt 0 0 = error "GHC.Real.gcdInt: gcd 0 0 is undefined"
gcdInt a b = fromIntegral (gcdInteger (fromIntegral a) (fromIntegral b))
#endif
More information about the Cvs-libraries
mailing list