Skip to content

fix: avoid cancellation near multiples of 2π in math/base/special/cosm1 - #16115

Draft
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/cosm1-near-two-pi
Draft

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/cosm1-near-two-pi

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • evaluates math/base/special/cosm1 as -2 sin^2(x/2) outside [-π/4,π/4] (JS and C), instead of cos(x) - 1.

cos(x) - 1 cancels wherever cos(x) is close to one, which is near every multiple of 2π, not only near zero (inside [-π/4,π/4] the polynomial already handles that):

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

cosm1( -6.271126315854843 );
// develop: -0.0000727087547741645
// this PR: -0.00007270875477419403 (256-bit BigFloat: -0.00007270875477419403)

x/2 is exact and sin has its own argument reduction, so the half-angle form has no cancellation. #15609 uses the same identity for versin (versin(x) = -cosm1(x)).

  • replaces the test for |x| > π/4, which compared cosm1(x) against cos(x) - 1 (the expression being replaced) at 1 ULP, with fixtures evaluated in 256-bit BigFloat by a new Julia runner. The fixtures cover the same two ranges, [π/4, 4π] and [-4π, -π/4], plus 2000 values within 10^-8 to 10^-1 of ±2πk for k = 1..10. develop is off by up to about 2^30 ULPs on those, and this PR passes all three at 2.

Related Issues

Does this pull request have any related issues?

None.

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 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

…sm1`

Outside `[-π/4,π/4]`, `cosm1` returned `cos(x) - 1`, which cancels where
`cos(x)` is close to one; near `x = 2πk` the result could be off by
about 2^30 ULPs. Use `-2 sin^2(x/2)` there instead, the same half-angle
identity as stdlib-js#15609 uses for `versin`.

The test for that range compared against `cos(x) - 1` itself; it now uses
fixtures evaluated in BigFloat, including values close to multiples of 2π.

Signed-off-by: Abhist17 <abhistkamle.170903@gmail.com>
@Abhist17
Abhist17 requested a review from a team October 13, 2026 13:35
@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/cosm1 $\\color{green}245/245$
$\\color{green}+100.00\\%$
$\\color{green}12/12$
$\\color{green}+100.00\\%$
$\\color{green}3/3$
$\\color{green}+100.00\\%$
$\\color{green}245/245$
$\\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:17
@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