X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FRatio.hs;h=f0440993a38a0bdd1452c1eb6698bb3027e8371b;hb=HEAD;hp=22f3abeeeff8661ee1dc5a620db4d98e9abc1fff;hpb=96327b0696bc28551fbdb303506c52b9ee70c95c;p=ghc-base.git diff --git a/Data/Ratio.hs b/Data/Ratio.hs index 22f3abe..f044099 100644 --- a/Data/Ratio.hs +++ b/Data/Ratio.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE CPP #-} + ----------------------------------------------------------------------------- -- | -- Module : Data.Ratio @@ -15,10 +17,10 @@ module Data.Ratio ( Ratio , Rational - , (%) -- :: (Integral a) => a -> a -> Ratio a - , numerator -- :: (Integral a) => Ratio a -> a - , denominator -- :: (Integral a) => Ratio a -> a - , approxRational -- :: (RealFrac a) => a -> a -> Rational + , (%) -- :: (Integral a) => a -> a -> Ratio a + , numerator -- :: (Integral a) => Ratio a -> a + , denominator -- :: (Integral a) => Ratio a -> a + , approxRational -- :: (RealFrac a) => a -> a -> Rational -- Ratio instances: -- (Integral a) => Eq (Ratio a) @@ -27,16 +29,16 @@ module Data.Ratio -- (Integral a) => Real (Ratio a) -- (Integral a) => Fractional (Ratio a) -- (Integral a) => RealFrac (Ratio a) - -- (Integral a) => Enum (Ratio a) + -- (Integral a) => Enum (Ratio a) -- (Read a, Integral a) => Read (Ratio a) - -- (Integral a) => Show (Ratio a) + -- (Integral a) => Show (Ratio a) ) where import Prelude #ifdef __GLASGOW_HASKELL__ -import GHC.Real -- The basic defns for Ratio +import GHC.Real -- The basic defns for Ratio #endif #ifdef __HUGS__ @@ -68,27 +70,27 @@ import Ratio (Ratio(..), (%), numerator, denominator, approxRational) -- and abs r' < d', and the simplest rational is q%1 + the reciprocal of -- the simplest rational between d'%r' and d%r. -approxRational :: (RealFrac a) => a -> a -> Rational -approxRational rat eps = simplest (rat-eps) (rat+eps) - where simplest x y | y < x = simplest y x - | x == y = xr - | x > 0 = simplest' n d n' d' - | y < 0 = - simplest' (-n') d' (-n) d - | otherwise = 0 :% 1 - where xr = toRational x - n = numerator xr - d = denominator xr - nd' = toRational y - n' = numerator nd' - d' = denominator nd' +approxRational :: (RealFrac a) => a -> a -> Rational +approxRational rat eps = simplest (rat-eps) (rat+eps) + where simplest x y | y < x = simplest y x + | x == y = xr + | x > 0 = simplest' n d n' d' + | y < 0 = - simplest' (-n') d' (-n) d + | otherwise = 0 :% 1 + where xr = toRational x + n = numerator xr + d = denominator xr + nd' = toRational y + n' = numerator nd' + d' = denominator nd' - simplest' n d n' d' -- assumes 0 < n%d < n'%d' - | r == 0 = q :% 1 - | q /= q' = (q+1) :% 1 - | otherwise = (q*n''+d'') :% n'' - where (q,r) = quotRem n d - (q',r') = quotRem n' d' - nd'' = simplest' d' r' d r - n'' = numerator nd'' - d'' = denominator nd'' + simplest' n d n' d' -- assumes 0 < n%d < n'%d' + | r == 0 = q :% 1 + | q /= q' = (q+1) :% 1 + | otherwise = (q*n''+d'') :% n'' + where (q,r) = quotRem n d + (q',r') = quotRem n' d' + nd'' = simplest' d' r' d r + n'' = numerator nd'' + d'' = denominator nd'' #endif