[commit: ghc] master: base: Normalize style of approxRational (c59d6da)

git at git.haskell.org git at git.haskell.org
Thu Nov 9 23:35:10 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/c59d6da8639fd88919090b29cf4e76c4d0d8bbde/ghc

>---------------------------------------------------------------

commit c59d6da8639fd88919090b29cf4e76c4d0d8bbde
Author: Ben Gamari <bgamari.foss at gmail.com>
Date:   Thu Nov 9 17:53:24 2017 -0500

    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


>---------------------------------------------------------------

c59d6da8639fd88919090b29cf4e76c4d0d8bbde
 libraries/base/Data/Ratio.hs | 47 +++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/libraries/base/Data/Ratio.hs b/libraries/base/Data/Ratio.hs
index 8517e48..8d19506 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''
 



More information about the ghc-commits mailing list