Skip to content

fix: avoid cancellation near sin(x) = 1 in math/base/special/coversin and hacoversin - #16116

Draft
Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/coversin-near-peak
Draft

Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/coversin-near-peak

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • evaluates math/base/special/coversin and math/base/special/hacoversin as cos(x)^2 / (1 + sin(x)) (halved for hacoversin) where sin(x) > 0 (JS and C), instead of 1 - sin(x).

1 - sin(x) cancels wherever sin(x) is close to one:

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

coversin( 26.706218176635318 );
// develop: 0.000003592862648615913
// this PR: 0.0000035928626486120986 (256-bit BigFloat: 0.0000035928626486120986)

1 - sin(x) = (1 - sin(x))(1 + sin(x)) / (1 + sin(x)) = cos(x)^2 / (1 + sin(x)). Near the cancellation, cos(x) is near a zero, which cos computes with full relative accuracy after its own argument reduction, and the denominator is near 2. Where sin(x) <= 0, 1 - sin(x) doesn't cancel and is unchanged.

  • regenerates the fixtures in 256-bit BigFloat. They came from Julia's Float64 1 .- sin.(x) (the expression being replaced) and were checked at 0 ULPs, so they pinned the cancellation. Against the exact values, develop is off by up to about 2^22 ULPs on medium_positive.json. This PR passes every set at 2 to 3 ULPs, and those are the new tolerances.
  • adds near_extrema.json, 4200 values within 10^-8 to 10^-1 of ±π/2 + 2πk for k = -10..10. develop is off by up to about 2^40 ULPs there; this PR passes at 2.
  • changes the coversin( 3.141592653589793/2.0 ) examples from 0.0 to ~0.0. The double nearest π/2 is not π/2, and 1 - sin of it is 1.87e-33, which the doctest now sees.

Related Issues

Does this pull request have any related issues?

None. The same change for covercos and hacovercos, where 1 + sin(x) cancels near sin(x) = -1, follows separately.

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

…sin` and `hacoversin`

`1 - sin(x)` cancels where `sin(x)` is close to one, by up to about 2^40
ULPs within 1e-8 of `π/2 + 2πk`. Where `sin(x) > 0`, evaluate it as
`cos(x)^2 / (1 + sin(x))` instead: `cos(x)` keeps its relative accuracy
near its zeros, and the denominator does not cancel.

The fixtures came from Julia's Float64 `1 .- sin.(x)`, the same
expression; they are now evaluated in BigFloat, with a new set close to
where `sin(x)` is one or minus one. `coversin( π/2 )` now returns
`1.87e-33`, the exact value for the double nearest `π/2`, so the
examples say `~0.0`.

Signed-off-by: Abhist17 <abhistkamle.170903@gmail.com>
@Abhist17
Abhist17 requested a review from a team October 13, 2026 13:38
@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
Signed-off-by: Abhist17 <abhistkamle.170903@gmail.com>
@stdlib-bot

stdlib-bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/coversin $\\color{green}174/174$
$\\color{green}+100.00\\%$
$\\color{green}7/7$
$\\color{green}+100.00\\%$
$\\color{green}2/2$
$\\color{green}+100.00\\%$
$\\color{green}174/174$
$\\color{green}+100.00\\%$
math/base/special/hacoversin $\\color{green}174/174$
$\\color{green}+100.00\\%$
$\\color{green}7/7$
$\\color{green}+100.00\\%$
$\\color{green}2/2$
$\\color{green}+100.00\\%$
$\\color{green}174/174$
$\\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