diff options
author | Ben Gamari <bgamari.foss@gmail.com> | 2017-11-09 17:53:24 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2017-11-09 18:31:21 -0500 |
commit | c59d6da8639fd88919090b29cf4e76c4d0d8bbde (patch) | |
tree | b8aa0b6a955510f9222582b4f8101a06cfd786a3 /libraries | |
parent | ce9a67784390735aa9749667934af49665b8402e (diff) | |
download | haskell-c59d6da8639fd88919090b29cf4e76c4d0d8bbde.tar.gz |
base: Normalize style of approxRational
Stumbled upon this odd bit of style while looking at #14425. Usually I
don't like to do this sort of reformatting, but this seemed like it
would be necessary in the course fo fixing #14425.
Reviewers: hvr
Subscribers: rwbarton, thomie
Differential Revision: https://phabricator.haskell.org/D4168
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/base/Data/Ratio.hs | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/libraries/base/Data/Ratio.hs b/libraries/base/Data/Ratio.hs index 8517e485ff..8d195063a5 100644 --- a/libraries/base/Data/Ratio.hs +++ b/libraries/base/Data/Ratio.hs @@ -47,27 +47,30 @@ import GHC.Real -- The basic defns for Ratio -- 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'' |