Skip to content

Commit 3c7f84a

Browse files
committed
Prevent NaN / Infinity for mod/div by 0 integers
1 parent 6d929d7 commit 3c7f84a

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/Data/EuclideanRing.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ exports.intDegree = function (x) {
88
// https://en.m.wikipedia.org/wiki/Modulo_operation.
99
exports.intDiv = function (x) {
1010
return function (y) {
11+
if (y == 0) return 0;
1112
return y > 0 ? Math.floor(x / y) : -Math.floor(x / -y);
1213
};
1314
};
1415

1516
exports.intMod = function (x) {
1617
return function (y) {
18+
if (y == 0) return 0;
1719
var yy = Math.abs(y);
1820
return ((x % yy) + yy) % yy;
1921
};

test/Test/Main.purs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ testOrd x y ord =
2929
nan :: Number
3030
nan = 0.0/0.0
3131

32-
-- Unfortunately, NaN inhabits our Int
33-
intNan :: Int
34-
intNan = mod 1 0
35-
3632
plusInfinity :: Number
3733
plusInfinity = 1.0/0.0
3834

@@ -60,7 +56,8 @@ testOrderings = do
6056
assert "NaN > 1 should be false" $ (nan > 1.0) == false
6157
assert "NaN < 1 should be false" $ (nan < 1.0) == false
6258
assert "NaN == 1 should be false" $ nan /= 1.0
63-
testOrd intNan 2147483647 GT
59+
testOrd (1 / 0) 0 EQ
60+
testOrd (mod 1 0) 0 EQ
6461
testOrd 'a' 'b' LT
6562
testOrd 'b' 'A' GT
6663
testOrd "10" "0" GT

0 commit comments

Comments
 (0)