Commit 9e3fb41b authored by Simon Marlow's avatar Simon Marlow
Browse files

Add RatioZeroDenominator to ArithException, and use it instead of error

As per discussion on the libraries list.
parent c8a8b066
......@@ -27,6 +27,7 @@ module GHC.Err
(
absentErr -- :: a
, divZeroError -- :: a
, ratioZeroDenominatorError -- :: a
, overflowError -- :: a
, error -- :: String -> a
......@@ -82,6 +83,10 @@ in the libraries before the Exception type has been defined yet.
divZeroError :: a
divZeroError = throw DivideByZero
{-# NOINLINE ratioZeroDenominatorError #-}
ratioZeroDenominatorError :: a
ratioZeroDenominatorError = throw RatioZeroDenominator
{-# NOINLINE overflowError #-}
overflowError :: a
overflowError = throw Overflow
......
......@@ -182,6 +182,7 @@ data ArithException
| LossOfPrecision
| DivideByZero
| Denormal
| RatioZeroDenominator
deriving (Eq, Ord, Typeable)
instance Exception ArithException
......@@ -192,5 +193,6 @@ instance Show ArithException where
showsPrec _ LossOfPrecision = showString "loss of precision"
showsPrec _ DivideByZero = showString "divide by zero"
showsPrec _ Denormal = showString "denormal"
showsPrec _ RatioZeroDenominator = showString "Ratio has zero denominator"
\end{code}
......@@ -92,7 +92,7 @@ their greatest common divisor.
\begin{code}
reduce :: (Integral a) => a -> a -> Ratio a
{-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
reduce _ 0 = error "Ratio.%: zero denominator"
reduce _ 0 = ratioZeroDenominatorError
reduce x y = (x `quot` d) :% (y `quot` d)
where d = gcd x y
\end{code}
......@@ -412,7 +412,7 @@ instance (Integral a) => Num (Ratio a) where
instance (Integral a) => Fractional (Ratio a) where
{-# SPECIALIZE instance Fractional Rational #-}
(x:%y) / (x':%y') = (x*y') % (y*x')
recip (0:%_) = error "Ratio.%: zero denominator"
recip (0:%_) = ratioZeroDenominatorError
recip (x:%y)
| x < 0 = negate y :% negate x
| otherwise = y :% x
......@@ -628,7 +628,7 @@ x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
| e > 0 = (n ^ e) :% (d ^ e)
| e == 0 = 1 :% 1
| n > 0 = (d ^ (negate e)) :% (n ^ (negate e))
| n == 0 = error "Ratio.%: zero denominator"
| n == 0 = ratioZeroDenominatorError
| otherwise = let nn = d ^ (negate e)
dd = (negate n) ^ (negate e)
in if even e then (nn :% dd) else (negate nn :% dd)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment