Skip to content

BUG: fix y1 for large inputs - #231

Merged
dschmitz89 merged 1 commit into
scipy:mainfrom
FireflySentinel:fix-y1-large-x
Jul 31, 2026
Merged

BUG: fix y1 for large inputs#231
dschmitz89 merged 1 commit into
scipy:mainfrom
FireflySentinel:fix-y1-large-x

Conversation

@FireflySentinel

Copy link
Copy Markdown
Contributor

Reference issue

Follow-up to j0 (#131), y0 (#140) and j1 (#143). y1 is the last one in that group still forming the phase as x - 3pi/4.

What does this implement/fix?

xn = x - THPIO4 rounds to the nearest double, so it costs about ulp(x)/2 of
absolute phase, which sin/cos turn into relative error.

I implemented the same rewrite as y0 (#140), so the trig is evaluated on the unmodified x. I also adds the
missing y1 case to test_bessel_functions_large_inputs.cpp.

Additional information

Relative error vs mpmath at 1000 dps:

x before after
1e4 1.1e-13 9.0e-18
1e8 1.4e-08 1.1e-16
1e20 6.7 1.2e-16

j0, y0 and j1 are at ~1e-16 on the same points, so this is y1 only.
Additionally, yv/yn inherit it through the recurrence. For example, yv(200, 1e7) is off by 4.1e-10 before this change.

pixi run tests passes and the new test fails on main.

AI Generation Disclosure

I used Claude Code to draft the fix and test.

y1 formed the asymptotic phase as the double-precision subtraction
`x - 3*pi/4`, which injects an absolute phase error of order ulp(x)/2 and
turns it into relative error in the result. Accuracy degrades from ~1e-13 at
x=1e4 to a wrong answer by a factor of 7.7 at x=1e20.

Applies the same trigonometric rewrite already used for j0 (scipy#131),
y0 (scipy#140) and j1 (scipy#143), which y1 was not carried over to:

    sin(x - 3pi/4) = -(sin x + cos x)/sqrt(2)
    cos(x - 3pi/4) =  (sin x - cos x)/sqrt(2)

so p*sin(x-3pi/4) + w*q*cos(x-3pi/4) = [(w*q-p)*sin x - (p+w*q)*cos x]/sqrt(2),
with the 1/sqrt(2) folded into SQRT2OPI -> SQRT1OPI exactly as y0 does. sin
and cos are then evaluated on the unmodified x, so no phase precision is lost.

Adds the missing y1 case to the large-input tests, which covered j0, y0 and
j1 but not y1.
@github-actions github-actions Bot added the Bug Something isn't working label Jul 31, 2026
@dschmitz89

Copy link
Copy Markdown
Contributor

Thanks @FireflySentinel !

@dschmitz89
dschmitz89 merged commit 46cecef into scipy:main Jul 31, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants