Skip to content

Commit 18fb35c

Browse files
author
Vladimir Ciobanu
authored
Fix HugeNum comparison (#20)
1 parent 29781e5 commit 18fb35c

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Breaking changes (😱!!!):
99
New features:
1010

1111
Bugfixes:
12+
- Fixed comparing negative `HugeNum` values and `HugeInt.toInt` returning `Nothing` for negative values ([#10](https://github.com/purescript-contrib/purescript-precise/issues/10))
1213

1314
Other improvements:
1415

src/Data/HugeNum.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ compareHugeNum :: HugeNum -> HugeNum -> Ordering
102102
compareHugeNum x@(HugeNum r1) y@(HugeNum r2)
103103
| r1.sign < r2.sign = LT
104104
| r1.sign > r2.sign = GT
105-
| r1.decimal > r2.decimal = GT
106-
| r1.decimal < r2.decimal = LT
105+
| r1.decimal > r2.decimal = if r1.sign == Minus then LT else GT
106+
| r1.decimal < r2.decimal = if r1.sign == Minus then GT else LT
107107
| x == y = EQ
108108
| otherwise = z where
109109
dec = r1.decimal

test/Main.purs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ module Test.Main where
22

33
import Prelude
44

5-
import Data.HugeNum (HugeNum)
6-
import Data.HugeNum.Gen (genHugeNum)
75
import Data.HugeInt (HugeInt)
6+
import Data.HugeInt as HI
87
import Data.HugeInt.Gen (genHugeInt)
8+
import Data.HugeNum (HugeNum)
9+
import Data.HugeNum as HN
10+
import Data.HugeNum.Gen (genHugeNum)
11+
import Data.Maybe (Maybe(..))
912
import Effect (Effect)
1013
import Effect.Console (log)
11-
import Test.QuickCheck (class Arbitrary)
14+
import Test.QuickCheck (class Arbitrary, quickCheck)
1215
import Test.QuickCheck.Laws.Data.Eq (checkEq)
1316
import Test.QuickCheck.Laws.Data.Ord (checkOrd)
1417
import Test.QuickCheck.Laws.Data.Ring (checkRing)
@@ -55,3 +58,13 @@ main = do
5558
checkOrd prxHugeInt
5659
checkSemiring prxHugeInt
5760
checkRing prxHugeInt
61+
log "Checking from/to Num...\n"
62+
quickCheck toFromNumIsIdentity
63+
log "Checking from/to Int...\n"
64+
quickCheck toFromIntIsIdentity
65+
66+
toFromNumIsIdentity :: Number -> Boolean
67+
toFromNumIsIdentity n = HN.toNumber (HN.fromNumber n) == n
68+
69+
toFromIntIsIdentity :: Int -> Boolean
70+
toFromIntIsIdentity n = HI.toInt (HI.fromInt n) == Just n

0 commit comments

Comments
 (0)