diff options
| author | 2020-03-28 10:23:28 -0500 | |
|---|---|---|
| committer | 2020-03-28 10:23:28 -0500 | |
| commit | ff163a5ae4b710228f7743acc83f84d5a69171f7 (patch) | |
| tree | 26496144b10c8c14d9bd6fe769c8c455cd823d95 | |
| parent | Address edge case of reduce2 when ind is empty. (diff) | |
Use modulo instead of remainder for even?/odd?.
Works better for negative and fractional numbers.
| -rw-r--r-- | src/boot/boot.janet | 4 | ||||
| -rw-r--r-- | test/suite1.janet | 22 |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 0a677370..4ddbbef2 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -79,8 +79,8 @@ # Basic predicates (defn nan? "Check if x is NaN" [x] (not= x x)) -(defn even? "Check if x is even." [x] (= 0 (% x 2))) -(defn odd? "Check if x is odd." [x] (not= 0 (% x 2))) +(defn even? "Check if x is even." [x] (= 0 (mod x 2))) +(defn odd? "Check if x is odd." [x] (= 1 (mod x 2))) (defn zero? "Check if x is zero." [x] (= x 0)) (defn pos? "Check if x is greater than 0." [x] (> x 0)) (defn neg? "Check if x is less than 0." [x] (< x 0)) diff --git a/test/suite1.janet b/test/suite1.janet index 5b9f4f4f..c3d625d6 100644 --- a/test/suite1.janet +++ b/test/suite1.janet @@ -259,4 +259,26 @@ (assert (array= (array/slice @[1 2 3] 0 2) @[1 2]) "array/slice 1") (assert (array= (array/slice @[0 7 3 9 1 4] 2 -2) @[3 9 1]) "array/slice 2") +# Even and odd + +(assert (odd? 9) "odd? 1") +(assert (odd? -9) "odd? 2") +(assert (not (odd? 10)) "odd? 3") +(assert (not (odd? 0)) "odd? 4") +(assert (not (odd? -10)) "odd? 5") +(assert (not (odd? 1.1)) "odd? 6") +(assert (not (odd? -0.1)) "odd? 7") +(assert (not (odd? -1.1)) "odd? 8") +(assert (not (odd? -1.6)) "odd? 9") + +(assert (even? 10) "even? 1") +(assert (even? -10) "even? 2") +(assert (even? 0) "even? 3") +(assert (not (even? 9)) "even? 4") +(assert (not (even? -9)) "even? 5") +(assert (not (even? 0.1)) "even? 6") +(assert (not (even? -0.1)) "even? 7") +(assert (not (even? -10.1)) "even? 8") +(assert (not (even? -10.6)) "even? 9") + (end-suite) |
