Skip to content

fix: round zeta BigNumber digit count to avoid non-integer factorial#3683

Open
xianjianlf2 wants to merge 1 commit into
josdejong:developfrom
xianjianlf2:fix/zeta-bignumber-digits-round-3532
Open

fix: round zeta BigNumber digit count to avoid non-integer factorial#3683
xianjianlf2 wants to merge 1 commit into
josdejong:developfrom
xianjianlf2:fix/zeta-bignumber-digits-round-3532

Conversation

@xianjianlf2

Copy link
Copy Markdown

Problem

The BigNumber branch of zeta derived its digit count from Math.abs(Math.log10(config.relTol)). When relTol is not an exact power of ten (e.g. 5e-8, or 1e-320 where floating-point rounding makes log10 non-integer), this produced a non-integer digit count. That value eventually reached factorial/gamma as a non-integer BigNumber, throwing Integer BigNumber expected.

Fix

Round the derived value with Math.round(...) so the digit count is always an integer, regardless of floating-point noise in Math.log10.

-        // relTol is for example 1e-12. Extract the positive exponent 12 from that
-        return Math.abs(Math.log10(config.relTol))
+        // relTol is for example 1e-12. Extract the positive exponent 12 from that.
+        // Round to guard against floating point errors in Math.log10 for very
+        // small relTol like 1e-320, which would otherwise yield a non-integer
+        // digit count (see https://github.com/josdejong/mathjs/issues/3532).
+        return Math.round(Math.abs(Math.log10(config.relTol)))

Testing

Added a unit test in test/unit-tests/function/special/zeta.test.js that configures BigNumber mode with a relTol whose base-10 logarithm is non-integer (5e-16) and asserts that zeta(3) no longer throws and returns the expected value.

Closes #3532

The BigNumber branch of zeta derived its digit count from
Math.abs(Math.log10(config.relTol)). When relTol is not an exact power
of ten (e.g. 5e-8, or 1e-320 where floating point rounding makes log10
non-integer), this produced a non-integer digit count that eventually
reached factorial/gamma as a non-integer BigNumber, throwing
"Integer BigNumber expected". Round the value so the digit count is
always an integer.

Closes josdejong#3532
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is there a bug with zeta(n) ?

1 participant