Commit dc487642 authored by taylorfausak's avatar taylorfausak Committed by Marge Bot

Implement `round` for `Ratio` that doesn't explode with `Natural`s

parent 73d6e508
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment