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