Skip to content

fix: avoid cancellation for large negative m in math/base/special/ellipk - #16109

Draft
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/ellipk-large-negative-m
Draft

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/ellipk-large-negative-m

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • forms 1 - x as 1/(1-m) in math/base/special/ellipk (JS and C), where x = m/(m-1) is the transformed parameter for m < 0, instead of subtracting x from one.

The subtraction cancels as m grows more negative, and below -2^53 x rounds to exactly one:

m develop this PR mpmath
-1e10 1.2899219784372459e-4 1.2899219825792643e-4 1.2899219825792638e-4
-1e15 5.899571246472065e-7 5.899444819017533e-7 5.899444819017532e-7
-3.7e15 3.190675447053322e-7 3.174519615424412e-7 3.174519615424411e-7
-1e16 Infinity 1.9806975105072258e-7 1.9806975105072255e-7
-1e300 Infinity 3.4677405831022676e-148 3.4677405831022676e-148

This also explains the FIXME: investigate the reason for this large discrepancy on the large-negative test (tolerance 204,773,163 ULPs). The C++ runner computes those fixtures with Boost through the same transformation (k = sqrt(m/(1+m))), so the expected values themselves were off by 1e-8 to 6e-8 relative to mpmath. The fixed implementation is off by 1e-16.

  • replaces test/fixtures/cpp/large_negative.json with a Julia runner that evaluates K(m) = π / (2 agm(1, sqrt(1-m))) in 2048-bit BigFloat. That identity holds for every m < 1 without transforming m. It writes large_negative.json (same range as before) and a new huge_negative.json (-1e10 to -1e300). The large-negative tolerance drops from 204,773,163 to 5 ULPs, the new set passes at 4, and the FIXME is gone.
  • keeps ellipk( -Infinity ) returning NaN, as documented.

Related Issues

Does this pull request have any related issues?

None. math/base/special/ellipe calls ellipk( x ) with the same rounded x for m < 0, and returns 1 below -2^53. I'll send that as a follow-up, since it depends on this.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

test.js and test.native.js: 17007 pass.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

…ellipk`

For `m < 0` the parameter is transformed to `x = m/(m-1)`, and `1 - x`
was then formed by subtraction, which cancels as `m` grows more negative.
The relative error reached 2e-5 at `m = -1e15`, 5e-3 at `-3.7e15`, and
`x` rounds to one below `-2^53`, so the function returned `Infinity`.
Form `1 - x` as `1/(1-m)` instead.

The C++ fixture for large negative values was computed through the same
transformation in Boost and was itself off by up to 6e-8, which is what
the 204773163-ULP tolerance and its FIXME were covering. It is replaced by
a Julia runner evaluating `pi/(2*agm(1, sqrt(1-m)))` in BigFloat.

Signed-off-by: Abhist17 <abhistkamle.170903@gmail.com>
@Abhist17
Abhist17 requested a review from a team October 13, 2026 13:23
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. labels Oct 13, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/ellipk $\\color{green}872/872$
$\\color{green}+100.00\\%$
$\\color{green}84/84$
$\\color{green}+100.00\\%$
$\\color{green}14/14$
$\\color{green}+100.00\\%$
$\\color{green}872/872$
$\\color{green}+100.00\\%$

The above coverage report was generated for the changes in this PR.

@Abhist17
Abhist17 marked this pull request as draft October 14, 2026 04:16
@stdlib-bot stdlib-bot removed the Needs Review A pull request which needs code review. label Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Math Issue or pull request specific to math functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants