X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FReal.lhs;h=971f2767b655b14f94255486a520f1b5a65f3106;hb=748003827970cfc57cb26db256b72d16e504bb37;hp=197bcde23a27f2a07de4923724c86ce390a5dc5e;hpb=06ffe41b5a1730c93f5e3add2d0f32fe6677d223;p=ghc-base.git diff --git a/GHC/Real.lhs b/GHC/Real.lhs index 197bcde..971f276 100644 --- a/GHC/Real.lhs +++ b/GHC/Real.lhs @@ -4,7 +4,7 @@ ----------------------------------------------------------------------------- -- | -- Module : GHC.Real --- Copyright : (c) The FFI Task Force, 1994-2002 +-- Copyright : (c) The University of Glasgow, 1994-2002 -- License : see libraries/base/LICENSE -- -- Maintainer : cvs-ghc@haskell.org @@ -24,6 +24,7 @@ import GHC.Num import GHC.List import GHC.Enum import GHC.Show +import GHC.Err infixr 8 ^, ^^ infixl 7 /, `quot`, `rem`, `div`, `mod` @@ -132,10 +133,15 @@ class (Real a, Enum a) => Integral a where -- | conversion to 'Integer' toInteger :: a -> Integer + {-# INLINE quot #-} + {-# INLINE rem #-} + {-# INLINE div #-} + {-# INLINE mod #-} n `quot` d = q where (q,_) = quotRem n d n `rem` d = r where (_,r) = quotRem n d n `div` d = q where (q,_) = divMod n d n `mod` d = r where (_,r) = divMod n d + divMod n d = if signum r == negate (signum d) then (q-1, r+d) else qr where qr@(q,r) = quotRem n d @@ -153,6 +159,8 @@ class (Num a) => Fractional a where -- @('Fractional' a) => a@. fromRational :: Rational -> a + {-# INLINE recip #-} + {-# INLINE (/) #-} recip x = 1 / x x / y = x * recip y @@ -173,13 +181,15 @@ class (Real a, Fractional a) => RealFrac a where properFraction :: (Integral b) => a -> (b,a) -- | @'truncate' x@ returns the integer nearest @x@ between zero and @x@ truncate :: (Integral b) => a -> b - -- | @'round' x@ returns the nearest integer to @x@ + -- | @'round' x@ returns the nearest integer to @x@; + -- the even integer if @x@ is equidistant between two integers round :: (Integral b) => a -> b -- | @'ceiling' x@ returns the least integer not less than @x@ ceiling :: (Integral b) => a -> b -- | @'floor' x@ returns the greatest integer not greater than @x@ floor :: (Integral b) => a -> b + {-# INLINE truncate #-} truncate x = m where (m,_) = properFraction x round x = let (n,r) = properFraction x @@ -337,8 +347,11 @@ instance (Integral a) => Show (Ratio a) where {-# SPECIALIZE instance Show Rational #-} showsPrec p (x:%y) = showParen (p > ratioPrec) $ showsPrec ratioPrec1 x . - showString "%" . -- H98 report has spaces round the % - -- but we removed them [May 04] + showString " % " . + -- H98 report has spaces round the % + -- but we removed them [May 04] + -- and added them again for consistency with + -- Haskell 98 [Sep 08, #1920] showsPrec ratioPrec1 y instance (Integral a) => Enum (Ratio a) where @@ -444,18 +457,21 @@ lcm _ 0 = 0 lcm 0 _ = 0 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' +"lcm/Integer->Integer->Integer" lcm = lcmInteger #-} --- XXX these optimisation rules are disabled for now to make it easier --- to experiment with other Integer implementations --- "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 +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 integralEnumFrom :: (Integral a, Bounded a) => a -> [a] integralEnumFrom n = map fromInteger [toInteger n .. toInteger (maxBound `asTypeOf` n)]