Make inequality comparison slightly more efficient
This commit is contained in:
parent
80e43be8fc
commit
e21b81b965
|
@ -1,5 +1,6 @@
|
|||
module Data.Ratio
|
||||
|
||||
import Data.Bool.Xor
|
||||
import Data.Maybe
|
||||
import Data.So
|
||||
import Data.IntegralGCD
|
||||
|
@ -102,12 +103,12 @@ Eq a => Eq (Ratio a) where
|
|||
export
|
||||
(Ord a, Num a) => Ord (Ratio a) where
|
||||
compare (MkRatio n d) (MkRatio m b) =
|
||||
flipIfNeg (b*d) $ compare (n*b) (m*d)
|
||||
flipIf (b >= 0 `xor` d >= 0) $ compare (n*b) (m*d)
|
||||
where
|
||||
flipIfNeg : a -> Ordering -> Ordering
|
||||
flipIfNeg x EQ = EQ
|
||||
flipIfNeg x LT = if x >= 0 then LT else GT
|
||||
flipIfNeg x GT = if x >= 0 then GT else LT
|
||||
flipIf : Bool -> Ordering -> Ordering
|
||||
flipIf _ EQ = EQ
|
||||
flipIf b LT = if b then GT else LT
|
||||
flipIf b GT = if b then LT else GT
|
||||
|
||||
export
|
||||
Show a => Show (Ratio a) where
|
||||
|
|
Loading…
Reference in a new issue