Skip to content

fix: avoid cancellation near one in math/base/special/asec - #16112

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

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/asec-near-one

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • evaluates math/base/special/asec as atan( sqrt((x-1)*(x+1)) ) for 1 <= x <= 2 (JS and C), instead of acos( 1/x ).

Near one the result is small, and acos( 1/x ) takes it from 1 - 1/x, whose low bits are lost to the rounding of 1/x:

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

asec( 1.000000001 );
// develop: 4.472136140385348e-5
// this PR: 4.472136138149279e-5 (256-bit BigFloat: 4.472136138149279e-5)

For x >= 1, tan( asec(x) ) = sqrt(x^2-1), and x-1 is exact for 1 <= x <= 2. Near -1 the result is close to π, so the relative error there was already small, and that side is unchanged.

  • regenerates the fixtures in 256-bit BigFloat (they came from Julia's Float64 asec). The existing sets still pass at their current tolerances.
  • adds near_one.json, 1000 values 1 + 10^k for k in [-15, -1], which pass at 2 ULPs.

Related Issues

Does this pull request have any related issues?

None. Same construction as #16110 (asech) and #16111 (acoth).

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

`acos( 1/x )` loses the low bits of `1-1/x` to the rounding of `1/x`, and
near one those bits are most of the result: `asec( 1.000000001 )` was off
by about 3.3e6 ULPs. For `1 <= x <= 2`, evaluate
`atan( sqrt((x-1)*(x+1)) )` instead, where `x-1` is exact.

The fixtures were generated with Julia's Float64 `asec`; they are now
evaluated in BigFloat, with a new set of values close to one.

Signed-off-by: Abhist17 <abhistkamle.170903@gmail.com>
@Abhist17
Abhist17 requested a review from a team October 13, 2026 13:28
@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/asec $\\color{green}166/166$
$\\color{green}+100.00\\%$
$\\color{green}8/8$
$\\color{green}+100.00\\%$
$\\color{green}2/2$
$\\color{green}+100.00\\%$
$\\color{green}166/166$
$\\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