Skip to content

fix: avoid cancellation near |x| = 1 in math/base/special/acoth - #16111

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

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/acoth-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/acoth as sign(x) * log1p( 2/(|x|-1) )/2 for 1 <= |x| <= 2 (JS and C), instead of atanh( 1/x ).

atanh( 1/x ) depends on 1 - |1/x|, and rounding 1/x loses its low bits:

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

acoth( 1.000000001 );
// develop: 10.708206467132994
// this PR: 10.708206467632994 (256-bit BigFloat: 10.708206467632994)

acoth(x) = log( (x+1)/(x-1) )/2 = log1p( 2/(x-1) )/2, and x-1 is exact for 1 <= x <= 2. acoth(±1) still returns ±Infinity. Elsewhere the function is unchanged.

  • regenerates the fixtures in 256-bit BigFloat. They were generated with Julia's Float64 acoth, which is also atanh(inv(x)), so the reference values carried the same error. Against the exact values, the medium and large sets need 2 ULPs (they were checked at 1). The large set covers 3 <= |x| <= 28, which this PR doesn't touch, so those 2-ULP differences were already there.
  • adds near_positive_one.json and near_negative_one.json, 1000 values each at ±(1 + 10^k) for k in [-15, -1]. They pass at 1 ULP.

Related Issues

Does this pull request have any related issues?

None. It is the same construction as #16110 (asech); asec and acsc follow.

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

`atanh( 1/x )` loses the low bits of `1-|1/x|` to the rounding of `1/x`:
`acoth( 1.000000001 )` was off by about 2.8e5 ULPs. For `1 <= |x| <= 2`,
evaluate `sign(x) * log1p( 2/(|x|-1) )/2` instead, where `|x|-1` is exact.

The fixtures were generated with Julia's Float64 `acoth`, which is also
`atanh(inv(x))`; they are now evaluated in BigFloat, with new sets of
values close to 1 and -1.

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