Skip to content

fix: handle boundary cases for divide and log functions#3645

Closed
orelbn wants to merge 1 commit intojosdejong:developfrom
orelbn:orelbn/fix-math-log-boundary-cases
Closed

fix: handle boundary cases for divide and log functions#3645
orelbn wants to merge 1 commit intojosdejong:developfrom
orelbn:orelbn/fix-math-log-boundary-cases

Conversation

@orelbn
Copy link
Contributor

@orelbn orelbn commented Feb 21, 2026

Description

Tests

  • Updated boundary expectations in:
    • test/unit-tests/function/arithmetic/divide.test.js
    • test/unit-tests/function/arithmetic/log.test.js
  • Added/covered key regressions:
    • log(0, 1)
    • log(-1, 1)
    • log(-1, -1)
    • log(0, 0)
    • Complex divide-by-zero paths like i/0, (1+i)/0, (1-i)/(0+0i).
  • Verified locally:
    • npx mocha test/unit-tests/function/arithmetic/log.test.js test/unit-tests/function/arithmetic/divide.test.js

Breaking Changes

  • No API signature changes.
  • Behavioral change in edge cases:
    • Some operations that previously returned directional infinities like Infinity + Infinity i now return Complex(Infinity, NaN).
    • Scalar ±Infinity / 0 now returns Complex(Infinity, NaN) instead of numeric ±Infinity.
  • Consumers asserting exact real/imag infinity signs in these boundary cases may need test updates.
  • Consumers expecting numeric return type for ±Infinity / 0 must adapt to Complex output in that boundary case.

Impact on Codebase

  • Positive consistency impact

    • Edge handling is centralized in src/function/arithmetic/divideScalar.js, reducing duplicated special-case logic in higher-level functions.
    • src/function/arithmetic/log.js remains generic (divideScalar(self(x), self(base))), improving maintainability.
  • Cross-function behavioral impact

    • Function paths using scalar Complex/Complex and number/number (±Infinity / 0) division now inherit normalized complex-infinity behavior.
    • This improves mathematical coherence for complex limits but can alter legacy outputs in rare edge cases.
  • Testing impact

    • Existing tests that encoded directional infinity for divide-by-zero were updated to undetermined-phase semantics.
    • Added regression coverage reduces risk of reintroducing inconsistent infinity behavior through future refactors.
  • Risk profile

    • Low-to-moderate runtime risk: scope is small and localized to scalar division branches.
    • Main risk is downstream expectation drift (user code/tests depending on old directional outputs or numeric type for ±Infinity / 0), not broad numerical regressions in finite-domain operations.

Note I reviewed the code and made sure to understand the issue to the best of my ability, but I mainly used agentic coding to try and solve the issue. If there is any fundamental misunderstanding or issue arising from this approach, please let me know.

@gwhitney
Copy link
Collaborator

@orelbn thank you so much for your interest and taking the time to generate a PR in an effort to address the long-standing issue #1277. However, the core of your PR consists of a "patch" to the behavior of Complex.js and we're not going to go down that path in math.js. If there is non-ideal behavior in Complex.js that is leading to this issue in mathjs, please file a report with Complex.js, and once the behavior is fixed there, we will update to the new release of Complex.js. Thanks! (and so closing this PR)

@gwhitney gwhitney closed this Feb 28, 2026
@orelbn
Copy link
Contributor Author

orelbn commented Mar 1, 2026

No problem, thanks for the feedback!

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.

2 participants