Ticket #3304: gcd_0_0_commit.patch

File gcd_0_0_commit.patch, 1.8 KB (added by daniel.is.fischer, 4 years ago)

Patch for GHC.Real

  • GHC/Real.lhs

    commit c902382e3645777bf92f98fd8b873c0591921e25
    Author: Daniel Fischer <[email protected]>
    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.
    
    diff --git a/GHC/Real.lhs b/GHC/Real.lhs
    index 17d0452..0b4d4d5 100644
    a b x ^^ n = if n >= 0 then x^n else recip (x^(negate n)) 
    520520                  in if even e then (nn :% dd) else (negate nn :% dd)
    521521
    522522-------------------------------------------------------
    523 -- | @'gcd' x y@ is the greatest (positive) integer that divides both @x@
     523-- | @'gcd' x y@ is the greatest (nonnegative) integer that divides both @x@
    524524-- and @y@; for example @'gcd' (-3) 6@ = @3@, @'gcd' (-3) (-6)@ = @3@,
    525 -- @'gcd' 0 4@ = @4@.  @'gcd' 0 0@ raises a runtime error.
     525-- @'gcd' 0 4@ = @4@.  @'gcd' 0 0@ = @0@.
    526526gcd             :: (Integral a) => a -> a -> a
    527 gcd 0 0         =  error "Prelude.gcd: gcd 0 0 is undefined"
    528527gcd x y         =  gcd' (abs x) (abs y)
    529528                   where gcd' a 0  =  a
    530529                         gcd' a b  =  gcd' b (a `rem` b)
    lcm x y = abs ((x `quot` (gcd x y)) * y) 
    539538#ifdef OPTIMISE_INTEGER_GCD_LCM
    540539{-# RULES
    541540"gcd/Int->Int->Int"             gcd = gcdInt
    542 "gcd/Integer->Integer->Integer" gcd = gcdInteger'
     541"gcd/Integer->Integer->Integer" gcd = gcdInteger
    543542"lcm/Integer->Integer->Integer" lcm = lcmInteger
    544543 #-}
    545544
    546 gcdInteger' :: Integer -> Integer -> Integer
    547 gcdInteger' 0 0 = error "GHC.Real.gcdInteger': gcd 0 0 is undefined"
    548 gcdInteger' a b = gcdInteger a b
    549 
    550545gcdInt :: Int -> Int -> Int
    551 gcdInt 0 0 = error "GHC.Real.gcdInt: gcd 0 0 is undefined"
    552546gcdInt a b = fromIntegral (gcdInteger (fromIntegral a) (fromIntegral b))
    553547#endif
    554548