From 209a0ebc1ee42d1dfec030279748a5009ecf36cb Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 15 Oct 2025 16:04:53 +0200 Subject: [PATCH 1/2] fix: calculate `log10` for a bigint with value zero (fixes #3539) --- src/utils/bigint.js | 2 +- test/unit-tests/function/arithmetic/log10.test.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/utils/bigint.js b/src/utils/bigint.js index 4a163cfdfa..5c02de3671 100644 --- a/src/utils/bigint.js +++ b/src/utils/bigint.js @@ -22,6 +22,6 @@ export function promoteLogarithm (log16, numberLog, config, cplx) { const s15 = s.substring(0, 15) return log16 * (s.length - s15.length) + numberLog(Number('0x' + s15)) } - return cplx(b.toNumber()) + return cplx(Number(b)) } } diff --git a/test/unit-tests/function/arithmetic/log10.test.js b/test/unit-tests/function/arithmetic/log10.test.js index e8e7682a4a..48ab8c8779 100644 --- a/test/unit-tests/function/arithmetic/log10.test.js +++ b/test/unit-tests/function/arithmetic/log10.test.js @@ -5,6 +5,7 @@ import { approxDeepEqual } from '../../../../tools/approx.js' import math from '../../../../src/defaultInstance.js' const mathPredictable = math.create({ predictable: true }) const complex = math.complex +const bignumber = math.bignumber const matrix = math.matrix const unit = math.unit const log10 = math.log10 @@ -40,7 +41,17 @@ describe('log10', function () { }) it('should return the log base 10 of zero', function () { - approxDeepEqual(log10(0), -Infinity) + assert.deepStrictEqual(log10(0), -Infinity) + assert.deepStrictEqual(log10(0n), complex(Infinity, Infinity)) + assert.deepStrictEqual(log10(bignumber('0')), bignumber('-Infinity')) + assert.deepStrictEqual(log10(false), -Infinity) + }) + + it('should return the log base 10 of zero with predicable:true', function () { + assert.deepStrictEqual(mathPredictable.log10(0), -Infinity) + assert.deepStrictEqual(mathPredictable.log10(0n), NaN) + assert.deepStrictEqual(mathPredictable.log10(bignumber('0')), bignumber('-Infinity')) + assert.deepStrictEqual(mathPredictable.log10(false), -Infinity) }) it('should return the log of positive bignumbers', function () { From e022c02bd65aa9fe0c75192781d708bddc6cabc2 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 15 Oct 2025 16:50:21 +0200 Subject: [PATCH 2/2] fix: return `-Infinity` instead of complex infinity for `0n` input in the log functions. --- src/utils/bigint.js | 5 +++++ test/unit-tests/function/arithmetic/log.test.js | 13 ++++++++++++- test/unit-tests/function/arithmetic/log10.test.js | 2 +- test/unit-tests/function/arithmetic/log2.test.js | 13 ++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/utils/bigint.js b/src/utils/bigint.js index 5c02de3671..f77c705570 100644 --- a/src/utils/bigint.js +++ b/src/utils/bigint.js @@ -22,6 +22,11 @@ export function promoteLogarithm (log16, numberLog, config, cplx) { const s15 = s.substring(0, 15) return log16 * (s.length - s15.length) + numberLog(Number('0x' + s15)) } + + if (b === 0n) { + return -Infinity + } + return cplx(Number(b)) } } diff --git a/test/unit-tests/function/arithmetic/log.test.js b/test/unit-tests/function/arithmetic/log.test.js index 3ad86017b2..f687c98f0c 100644 --- a/test/unit-tests/function/arithmetic/log.test.js +++ b/test/unit-tests/function/arithmetic/log.test.js @@ -5,6 +5,7 @@ import { approxDeepEqual } from '../../../../tools/approx.js' import math from '../../../../src/defaultInstance.js' const mathPredictable = math.create({ predictable: true }) const complex = math.complex +const bignumber = math.bignumber const matrix = math.matrix const unit = math.unit const fraction = math.fraction @@ -36,7 +37,17 @@ describe('log', function () { }) it('should return the log of zero', function () { - approxDeepEqual(log(0), -Infinity) + assert.deepStrictEqual(log(0), -Infinity) + assert.deepStrictEqual(log(0n), -Infinity) + assert.deepStrictEqual(log(bignumber('0')), bignumber('-Infinity')) + assert.deepStrictEqual(log(false), -Infinity) + }) + + it('should return the log of zero with predicable:true', function () { + assert.deepStrictEqual(mathPredictable.log(0), -Infinity) + assert.deepStrictEqual(mathPredictable.log(0n), NaN) + assert.deepStrictEqual(mathPredictable.log(bignumber('0')), bignumber('-Infinity')) + assert.deepStrictEqual(mathPredictable.log(false), -Infinity) }) it('should return the log base N of a number', function () { diff --git a/test/unit-tests/function/arithmetic/log10.test.js b/test/unit-tests/function/arithmetic/log10.test.js index 48ab8c8779..547d718b29 100644 --- a/test/unit-tests/function/arithmetic/log10.test.js +++ b/test/unit-tests/function/arithmetic/log10.test.js @@ -42,7 +42,7 @@ describe('log10', function () { it('should return the log base 10 of zero', function () { assert.deepStrictEqual(log10(0), -Infinity) - assert.deepStrictEqual(log10(0n), complex(Infinity, Infinity)) + assert.deepStrictEqual(log10(0n), -Infinity) assert.deepStrictEqual(log10(bignumber('0')), bignumber('-Infinity')) assert.deepStrictEqual(log10(false), -Infinity) }) diff --git a/test/unit-tests/function/arithmetic/log2.test.js b/test/unit-tests/function/arithmetic/log2.test.js index 332636dded..a743fbea37 100644 --- a/test/unit-tests/function/arithmetic/log2.test.js +++ b/test/unit-tests/function/arithmetic/log2.test.js @@ -5,6 +5,7 @@ import { approxDeepEqual } from '../../../../tools/approx.js' import math from '../../../../src/defaultInstance.js' const mathPredictable = math.create({ predictable: true }) const complex = math.complex +const bignumber = math.bignumber const matrix = math.matrix const unit = math.unit const log2 = math.log2 @@ -38,7 +39,17 @@ describe('log2', function () { }) it('should return the log base 2 of zero', function () { - approxDeepEqual(log2(0), -Infinity) + assert.deepStrictEqual(log2(0), -Infinity) + assert.deepStrictEqual(log2(0n), -Infinity) + assert.deepStrictEqual(log2(bignumber('0')), bignumber('-Infinity')) + assert.deepStrictEqual(log2(false), -Infinity) + }) + + it('should return the log base 2 of zero with predicable:true', function () { + assert.deepStrictEqual(mathPredictable.log2(0), -Infinity) + assert.deepStrictEqual(mathPredictable.log2(0n), NaN) + assert.deepStrictEqual(mathPredictable.log2(bignumber('0')), bignumber('-Infinity')) + assert.deepStrictEqual(mathPredictable.log2(false), -Infinity) }) it('should return the log of positive bignumbers', function () {