Skip to content

Commit

Permalink
Add missing instance
Browse files Browse the repository at this point in the history
  • Loading branch information
augustss committed Sep 26, 2024
1 parent eaa2e8d commit 6007c49
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Data/Ratio.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Data.Num
import Data.Ord
import Data.Ratio_Type
import Data.Real
import Data.RealFrac
import Text.Show

{- in Data.Ratio_Type
Expand Down Expand Up @@ -98,3 +99,16 @@ numerator (x :% _) = x

denominator :: forall a . Ratio a -> a
denominator (_ :% y) = y

instance (Ord a, Integral a) => RealFrac (Ratio a) where
properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
where (q, r) = quotRem x y
round r =
let
(n, f) = properFraction r
x = if r < 0 then -1 else 1
in case (compare (abs f) 0.5, odd n) of
(LT, _) -> n
(EQ, False) -> n
(EQ, True) -> n + x
(GT, _) -> n + x

0 comments on commit 6007c49

Please sign in to comment.