← Back to Pull requests

Add `asinpi`, `acospi` and `atanpi` #63031

Open
unknown opened this pull request with 631 lines diff across 7 files
View on GitHub
unknown unknown commented on Sep 05, 2026 17:37 UTC
Author

This PR changed had an interesting course. It started from asinpi, acospi, atanpi should be more efficient than their non-pi counterparts. It is now at: No, they are slower, but more accurate. And it turned out that the tested approaches to get the improved accuracy with at least similar performance are actually useful for the non-pi functions, too. So there are some interesting changes for asin, acos and atan included, too.

This PR is therefore probably more a collection of some changes with interesting properties all related to inverse-trig where it makes sense to decide on a case-by-case basis whether they should be included.

:robot: with :person_bald: :

What

asinpi(x), acospi(x), atanpi(y) and atanpi(y, x) compute the inverse trigonometric functions in half-turns. They complete the sinpi/cospi/tanpi family and correspond to the operations of the same names in C23 and IEEE 754-2019.

Without that, code working in half-turns (τ/2 = π) has to write asin(x)/pi. That leads to a larger error:

julia> asinpi(0.5)      # asin(0.5) is π/6, a sixth of a half-turn
0.16666666666666666

julia> asin(0.5)/pi     # off by one ulp
0.16666666666666669

Why it is worth having

Writing asin(x)/pi rounds twice, once in the function and once in the division, and reaches 1.7 ulp. Scaling by a double-double 1/π inside the kernel rounds once.

The measurements use Float64, 200 000 random arguments and a reference computed at 320-bit precision. The baseline is f(x)/pi built on the improved kernels in this PR, i.e. what is available without the pi-variants. The measurements use unsorted and sorted data, as the branch predictor has a heavy influence and there are probably use cases where angles are very similar from call to call and other uses cases where they change randomly.

| function | max ulp | misrounded / 200 000 | ns unsorted | ns sorted |
|---|---|---|---|---|
| asinpi | 1.520 → 0.695 | 65 971 → 2 762 (24x fewer) | 7.839 → 8.345 | 4.166 → 4.729 |
| acospi | 1.653 → 0.568 | 68 038 → 1 183 (58x fewer) | 9.411 → 10.025 | 4.279 → 4.302 |
| atanpi | 1.722 → 0.865 | 72 211 → 6 242 (12x fewer) | 8.131 → 8.307 | 5.335 → 5.371 |

The two-argument atanpi(y, x) is exact across the whole quadrant table, because the π/2, π and atan(1) that the radian version adds become 1/2, 1 and 1/4, all exactly representable.

What it costs

0 to 14% more time. And the three new exported names in Base, which IEEE recommends.

Also included

BigFloat dispatches to MPFR, which has provided these since 4.2.

two_sqrt (a double-double square root) and its use in asin and acos, which the new functions build on, plus a branchless argument reduction for atan.
Same method and sample as above; asin and acos are measured on [-1, 1] and atan on |x| ∈ [1/64, 64] on a log scale, which spreads points over the small-|x| branch and all four reduction centres. Noise floor 0.1% unsorted, 2.5% sorted.

| function | max ulp | misrounded / 200 000 | ns unsorted | ns sorted |
|---|---|---|---|---|
| asin | 0.813 → 0.623 | 11 669 → 2 771 | 6.560 → 6.144 | 4.493 → 4.271 |
| acos | 0.853 → 0.769 | 15 439 → 15 420 | 7.599 → 7.419 | 3.106 → 3.156 |
| atan | 0.814 → 0.814 | 7 246 → 7 180 | 8.995 → 7.222 | 4.349 → 4.834 |

asin improves on every axis. acos tightens only its worst case: the misrounding count is hardly changed and its timings sit within the noise floor, so it is probably no speedup.

atan is accuracy-neutral, and that commit is purely a throughput trade. The reduction itself is bit-identical, verified on all 4 278 190 082 non-NaN Float32 arguments. The commit also fuses two multiply-adds, which changes the result on roughly 0.2% of arguments without moving the maximum error. The two-argument
atan(y, x) is not modified, but it evaluates atan(abs(y/x)) and so inherits both effects, diluted by the division and quadrant handling around the call:

| function | differing results | ns unsorted | ns sorted |
|---|---|---|---|
| atan | ~0.2% | 8.995 → 7.222 (−20%) | 4.349 → 4.834 (+11%) |
| atan(y, x) | 500 / 400 000 | 20.468 → 18.265 (−10.8%) | 12.627 → 13.081 (+3.6%) |

Everything layered on the one-argument form, such as atand and acot, inherits the same trade. That commit can be dropped. However, atanpi uses its helper, so dropping needs a small follow-up.

On the measurements

Timings are the median of 41 rounds; each round uses a freshly generated input array and is preceded by a pass of unpredictable branches that scrambles the branch history, with the data cache re-warmed before timing. Measuring the same function under two names puts the noise floor below 3% throughout. This matters here: taking the minimum over repeated passes of one fixed array lets the predictor memorise the sequence, which reverses the sign of the atan result.

Claude 5 supported in creating this PR.

Diff: 631 lines (+473 / -158) across 7 files Reviewer: PatrickHaecker Review duration: 124466m
All Vouch review confidence checks passed
Residual risk 0.32 = Change Risk (0.57) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 631 lines diff across 7 files
0.57
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
44%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.386
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #63031"
general weight: 0.35
Assignees
Unassigned
Labels
risk: low confidence: 44.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 57.4% (MEDIUM) · Higher = larger blast radius
57.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 44.7% · Lower = rushed or shallow review
44.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 31.7% · Safe to merge
31.7%