Skip to content

Commit 153fe77

Browse files
fix: #3631 support bigint in compareNatural (#3632)
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
1 parent 31e6909 commit 153fe77

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ Richard Taylor <richard.taylor@claconnect.com>
282282
NilsDietrich <61544566+NilsDietrich@users.noreply.github.com>
283283
anslem chibuike <144047596+AnslemHack@users.noreply.github.com>
284284
Ayomide Bamigbade <iamtryve@gmail.com>
285+
Dheemanth07 <dheemanth1007@gmail.com>
285286
Anadian <willanad@yandex.com>
286287

287288
# Generated by tools/update-authors.js

HISTORY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
# unreleased changes since 15.1.0
44

5-
- Doc: Correct several arithmetic and relational documentation examples
5+
- Fix: #3631 Handle bigints in `compareNatural` (#3632). Thanks @Dheemanth07.
6+
- Docs: Correct several arithmetic and relational documentation examples
67
and add History (#3630). Thanks @Anadian.
78
- Fix: #3578 interpret empty true-expr of conditional as error (#3581).
89
Thanks @gwhitney.

src/function/relational/compareNatural.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const createCompareNatural = /* #__PURE__ */ factory(name, dependencies,
7676
*
7777
* History:
7878
*
79+
* v15.2 Handle bigints
7980
* v3.14 Created
8081
*
8182
*
@@ -92,8 +93,8 @@ export const createCompareNatural = /* #__PURE__ */ factory(name, dependencies,
9293
let c
9394

9495
// numeric types
95-
if ((typeX === 'number' || typeX === 'BigNumber' || typeX === 'Fraction') &&
96-
(typeY === 'number' || typeY === 'BigNumber' || typeY === 'Fraction')) {
96+
if ((typeX === 'number' || typeX === 'BigNumber' || typeX === 'Fraction' || typeX === 'bigint') &&
97+
(typeY === 'number' || typeY === 'BigNumber' || typeY === 'Fraction' || typeY === 'bigint')) {
9798
c = compare(x, y)
9899
if (c.toString() !== '0') {
99100
// c can be number, BigNumber, or Fraction

test/unit-tests/function/relational/compareNatural.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ describe('compareNatural', function () {
6464
assert.strictEqual(compareNatural(math.add(math.fraction(0.1), math.fraction(0.2)), math.fraction(0.3)).valueOf(), 0) // this would fail with numbers
6565
})
6666

67+
it('should compare bigints', function () {
68+
assert.strictEqual(compareNatural(3n, 10n), -1)
69+
assert.strictEqual(compareNatural(10n, 3n), 1)
70+
assert.strictEqual(compareNatural(3n, 3n), 0)
71+
})
72+
6773
it('should compare two measures of the same unit', function () {
6874
assert.strictEqual(compareNatural(unit('100cm'), unit('10inch')), 1)
6975
assert.strictEqual(compareNatural(unit('99cm'), unit('1m')), -1)
@@ -117,6 +123,12 @@ describe('compareNatural', function () {
117123
assert.strictEqual(compareNatural(bignumber(2), 2), -1)
118124
assert.strictEqual(compareNatural(2, bignumber(2)), 1)
119125

126+
// numbers and bigints
127+
assert.strictEqual(compareNatural(2n, 3), -1)
128+
assert.strictEqual(compareNatural(3, 2n), 1)
129+
assert.strictEqual(compareNatural(2n, 2), -1)
130+
assert.strictEqual(compareNatural(2, 2n), 1)
131+
120132
// array, DenseMatrix, SparseMatrix
121133
assert.strictEqual(compareNatural([2], matrix([2])), -1)
122134
assert.strictEqual(compareNatural(matrix([2]), [2]), 1)

0 commit comments

Comments
 (0)