Skip to content

fix: avoid cancellation near sin(x) = -1 in math/base/special/covercos and hacovercos - #16117

Draft
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/covercos-near-trough
Draft

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/covercos-near-trough

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

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

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

covercos( -1.5918310322213998 );
// develop: 0.0002212212592344054
// this PR: 0.00022122125923442057 (256-bit BigFloat: 0.00022122125923442057)

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, 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)). This PR passes every set at 2 to 3 ULPs, and those are the new tolerances.
  • adds near_extrema.json (values within 10^-8 to 10^-1 of ±π/2 + 2πk), which passes at 2 ULPs.

Related Issues

Does this pull request have any related issues?

None. Companion to #16116.

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

…rcos` and `hacovercos`

`1 + sin(x)` cancels where `sin(x)` is close to minus one. Where
`sin(x) < 0`, evaluate it as `cos(x)^2 / (1 - sin(x))` instead, as
stdlib-js#16116 does for `coversin` and `hacoversin`.

The fixtures came from Julia's Float64 `1 .+ sin.(x)`; they are now
evaluated in BigFloat, with a new set close to where `sin(x)` is one or
minus one.

Signed-off-by: Abhist17 <abhistkamle.170903@gmail.com>
@Abhist17
Abhist17 requested a review from a team October 13, 2026 13:40
@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/covercos $\\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/hacovercos $\\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