Skip to content

fix: avoid cancellation in math/base/special/vercos and havercos - #15611

Open
Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/vercos-havercos-accuracy
Open

Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/vercos-havercos-accuracy

Conversation

@Abhist17

@Abhist17 Abhist17 commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

var vercos = require( '@stdlib/math/base/special/vercos' );

vercos( Math.PI + 1.0e-6 ); // 5.000444502911705e-13 (correct to 2 ULP: 5.000000000172716e-13)

1 + cos(x) cancels when cos(x) is close to -1, so the relative error grows like 2e-16/(x - (2k+1)pi)^2, and right next to an odd multiple of pi no digits are correct. The fix uses the exact half-angle identities:

  • vercos(x) = 2*cos(x/2)^2
  • havercos(x) = cos(x/2)^2

x/2 is exact and cos handles its own argument reduction, so both functions are within 2 ULP of a 256-bit reference on every fixture, including huge |x|.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

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.

  • Fixtures: the runners used to evaluate 1.0 .+ cos.( x ) in double precision, which recorded the cancellation. They now evaluate the same textbook formula at 4096 bits and round to Float64, independent of the identity the implementation uses. There is a new near_odd_pi.json (4,000 points at (2k+1)*pi +/- d for k in -10:9 and d log-spaced over [1e-12, 0.1]). On develop, 3,990 of those 4,000 points are more than 2 ULP off. With this change, all of them are within 2 ULP. The JS fixture tests now allow 2 ULP instead of exact equality. The native tests already allowed 2 ULP, and 8 for huge values.
  • All tests pass in test.js and test.native.js for both packages, and the EditorConfig lint is clean.

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

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

This PR was written primarily by Claude Code. The bug was found by comparing every stats/base/dists function against SciPy on random parameters.


@stdlib-js/reviewers

`1 + cos(x)` loses significant digits as `cos(x)` approaches `-1`, i.e.,
for `x` near odd multiples of pi: `vercos( Math.PI + 1e-6 )` returned
`5.0004e-13` for a value of `5.0000e-13`, and closer to pi the result
has no correct digits. Use the half-angle identities
`vercos(x) = 2*cos(x/2)^2` and `havercos(x) = cos(x/2)^2`, which are
within 2 ULP everywhere. Regenerate the fixtures in extended precision
and add fixtures near odd multiples of pi.
@Abhist17
Abhist17 requested a review from a team September 27, 2026 23:56
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. labels Sep 27, 2026
@stdlib-bot

stdlib-bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/havercos $\\color{green}181/181$
$\\color{green}+0.00\\%$
$\\color{green}5/5$
$\\color{green}+0.00\\%$
$\\color{green}2/2$
$\\color{green}+0.00\\%$
$\\color{green}181/181$
$\\color{green}+0.00\\%$
math/base/special/vercos $\\color{green}181/181$
$\\color{green}+0.00\\%$
$\\color{green}5/5$
$\\color{green}+0.00\\%$
$\\color{green}2/2$
$\\color{green}+0.00\\%$
$\\color{green}181/181$
$\\color{green}+0.00\\%$

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

@kgryte

kgryte commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

@anandkaranubc Mind taking a look at this? Quick glance suggests some clean-up is needed:

  1. Implementation method moved from source and added to JSDoc.
  2. Use of abs2.
  3. Empty lines separating comments and expressions in Julia.
  4. ...etc.

Furthermore, I am not sure about the test fixture generation approach. Simply hardcoding the half-angle implementation when generating test fixtures seems incorrect. Instead, we should compute the quantities at higher-precision using the textbook formula, truncate to double, and then compare that we, in fact, retain and improve accuracy by moving to using the identity. Stated otherwise, use of the identity is an implementation detail. We should NOT be generating test fixtures which simply regurgitate that identity. That degrades the value of the tests.

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Sep 28, 2026
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Sep 28, 2026
- document the half-angle method in the JSDoc and C doc comments instead of an inline comment
- use `abs2` to square
- generate fixtures from the textbook formula `1 + cos(x)` evaluated at 4096 bits, rather than from the identity used by the implementation, and separate comments from expressions in the Julia runners
@Abhist17

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 9441496:

  1. The method is now documented under ## Method in the JSDoc and in the C doc comment, and the inline comment is gone.
  2. The functions now return 2.0 * abs2( cos( x / 2.0 ) ) and abs2( cos( x / 2.0 ) ), and the C versions use stdlib_base_abs2.
  3. Agreed on the fixtures. The runners now evaluate the textbook formula, 1 + cos(x), at 4096 bits (setprecision( BigFloat, 4096 )) and round to double. The identity no longer appears in the fixture generation. That is enough precision that the cancellation doesn't affect the reference even at the points closest to odd multiples of pi, and the regenerated values are the same doubles as before. Against them, the implementation is within 2 ULP on every fixture, while develop misses 3,990 of the 4,000 near_odd_pi points. I also added the empty lines between comments and expressions in the Julia runners.

I made the same changes to #15609 (814681b).

Abhist17 added a commit to Abhist17/stdlib that referenced this pull request Sep 28, 2026
Move the explanation of the evaluation method out of the function body,
as requested in review of stdlib-js#15611.

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. Needs Changes Pull request which needs changes before being merged. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants