Skip to content

Commit

Permalink
fix odd? and even? for non integers #242
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 14, 2024
1 parent d1eee4d commit 15fd687
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* fix macro expand on let with more than one binding
* fix shallow `list->array`
* fix resolving promises inside quoted promise realm
* fix undocumented symbol syntax extensions
* fix odd? even? on non integers

## 1.0.0-beta.17
### Breaking
Expand Down
14 changes: 9 additions & 5 deletions dist/lips.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/lips.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -5362,10 +5362,14 @@ LNumber.prototype.isOdd = function() {
if (this.isBigNumber()) {
return this.__value__ % BigInt(2) === BigInt(1);
}
if (this.__type__ === 'float') {
throw new Error('Invalid number float');
}
return this.__value__ % 2 === 1;
} else if (LNumber.isBN(this.__value__)) {
return this.__value__.isOdd();
}
throw new Error(`Invalid number ${this.__type__}`);
};
// -------------------------------------------------------------------------
LNumber.prototype.isEven = function() {
Expand Down
16 changes: 16 additions & 0 deletions tests/numbers.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1006,3 +1006,19 @@
(0.5i +0.46211715726000974i)
(1/2+1/2i 0.40389645531602575+0.5640831412674985i)
(+1/2i +0.46211715726000974i)))))

(test "numbers: should calculate odd? / even?"
(lambda (t)
(t.is (odd? 1) #t)
(t.is (odd? 2) #f)
(t.is (even? 10) #t)
(t.is (even? 21) #f)))

(test "numbers: should throw exception odd? / event?"
(lambda (t)
(for-each (lambda (op?)
(t.is (to.throw (op? 10+10i)) #t)
(t.is (to.throw (op? 1/2)) #t)
(t.is (to.throw (op? 1.2)) #t)
(t.is (to.throw (op? 1.2)) #t))
'(even? odd?))))

0 comments on commit 15fd687

Please sign in to comment.