Skip to content

Conversation

@philpeng1
Copy link

Fixes #3539

Summary:

  • Fix bigint logarithms to honor configuration and avoid crashes.
  • When number: 'bigint' and numberFallback: 'BigNumber', convert bigint inputs to BigNumber
  • Handle 0n in the bigint log helper and avoid calling non-existent BigInt methods.

Details

  • When number: 'bigint' and numberFallback: 'BigNumber', convert bigint inputs to BigNumber and use:
    log: x.ln()
    log10: x.log()
    log2: x.log(2)

  • Otherwise, use the bigint promotion helper for number results.

  • In the bigint promotion helper:
    Return numberLog(0) for 0n.
    For negatives: NaN when predictable: true, else compute complex via number downgrade.
    Avoid calling non-existent BigInt methods.

Tests:

Added regression tests:

  • log10(0n) returns -Infinity (no crash).
  • With number: 'bigint' and numberFallback: 'BigNumber':
    log(123n) returns BigNumber.
    log10(10n) -> BigNumber(1), log2(1024n) -> BigNumber(10).
    floor(log10(1n)) -> BigNumber(0) (no round error).
    Negative bigint: predictable true -> NaN; predictable false -> Complex.
  • With numberFallback: 'number':
    log2(2n70n) -> 70, log10(10n16n) -> 16.

@gwhitney
Copy link
Collaborator

Thanks so much for your interest in mathjs and for your efforts!

Apologies, but there is already a PR (from the project owner) for part of #3539: see #3555. You can either close this one, or mark it WIP, and we can revisit the other aspects after #3555 lands. But be aware that towards the end of the (lengthy!) discussion for #3374, we decided that the numberFallback config option is just for string input of decimal numbers, and that we would create a new config option numberApproximate to select the type for irrational or otherwise not exactly computable results. (The use case being that you want decimal input interpreted as exact fractions but want taking square roots (for example) to produce BigNumbers.

@philpeng1
Copy link
Author

Thanks for the explanation! Bit of a blunder from my part, didn't read everything through. I'll mark this as Closed for now.. Thanks for maintaining this project!

@philpeng1 philpeng1 closed this Oct 29, 2025
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.

Incompatibility between number: bigint and high-precision floating point arithmetic

2 participants