summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaylorfausak <taylor@fausak.me>2019-10-03 22:24:55 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2019-11-01 04:54:47 -0400
commitdc4876421386b406dd43a6015f09d92c3e9fb7d8 (patch)
tree42c30135affe34227c841cc77a6ec9c9de867aa5
parent73d6e508e6ce0b66bd082419effc0010b1dc9668 (diff)
downloadhaskell-dc4876421386b406dd43a6015f09d92c3e9fb7d8.tar.gz
Implement `round` for `Ratio` that doesn't explode with `Natural`s
-rw-r--r--libraries/base/GHC/Real.hs7
1 files changed, 7 insertions, 0 deletions
diff --git a/libraries/base/GHC/Real.hs b/libraries/base/GHC/Real.hs
index 10d8a54504..67ccf77596 100644
--- a/libraries/base/GHC/Real.hs
+++ b/libraries/base/GHC/Real.hs
@@ -508,6 +508,13 @@ instance (Integral a) => RealFrac (Ratio a) where
{-# SPECIALIZE instance RealFrac Rational #-}
properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
where (q,r) = quotRem x y
+ round r =
+ let (n, f) = properFraction r
+ in case (compare (abs f) 0.5, odd n) of
+ (LT, _) -> n
+ (EQ, False) -> n
+ (EQ, True) -> n + signum n
+ (GT, _) -> n + signum n
-- | @since 2.0.1
instance (Show a) => Show (Ratio a) where