From dc4876421386b406dd43a6015f09d92c3e9fb7d8 Mon Sep 17 00:00:00 2001 From: taylorfausak Date: Thu, 3 Oct 2019 22:24:55 -0400 Subject: Implement `round` for `Ratio` that doesn't explode with `Natural`s --- libraries/base/GHC/Real.hs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'libraries') 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 -- cgit v1.2.1