Commit 4d188ce6a4 for golang
commit 4d188ce6a41467bf4c4de0b830aa837dc33cbb68
Author: ICHINOSE Shogo <shogo82148@gmail.com>
Date: Tue Dec 1 12:38:32 2026 +0000
math: fix sign of J1 for small negative arguments
J1 records the sign of x and negates x so that it is positive,
then restores the sign before returning.
However, the branch for |x| < 2**-27 returned 0.5*x
without restoring the sign, so J1(x) returned a positive value
for -2**-27 < x < 0.
Jn(1, x) and Jn(-1, x) call J1 and were affected as well.
Restore the sign in that branch,
and add a test that J1, Jn(1, x) and Jn(-1, x) are odd.
Fixes #82101
Change-Id: Ib92815826340a4839665fb36878f503eb9ba0993
GitHub-Last-Rev: ee1c2c390d7b5457beb02d6a23a703648244c8ee
GitHub-Pull-Request: golang/go#82103
Reviewed-on: https://go-review.googlesource.com/c/go/+/847626
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/src/math/all_test.go b/src/math/all_test.go
index 143f00e10e..7b4b5a8d13 100644
--- a/src/math/all_test.go
+++ b/src/math/all_test.go
@@ -2825,6 +2825,19 @@ func TestJ1(t *testing.T) {
t.Errorf("J1(%g) = %g, want %g", vfj0SC[i], f, j1SC[i])
}
}
+
+ // https://go.dev/issue/82101
+ for _, x := range []float64{SmallestNonzeroFloat64, 1e-30, 1e-9, 1.0 / (1 << 27), 1e-4, 1, 2.5, 1e10} {
+ if f, g := J1(-x), -J1(x); !alike(f, g) {
+ t.Errorf("J1(%g) = %g, want %g", -x, f, g)
+ }
+ if f, g := Jn(1, -x), -J1(x); !alike(f, g) {
+ t.Errorf("Jn(1, %g) = %g, want %g", -x, f, g)
+ }
+ if f, g := Jn(-1, x), -J1(x); !alike(f, g) {
+ t.Errorf("Jn(-1, %g) = %g, want %g", x, f, g)
+ }
+ }
}
func TestJn(t *testing.T) {
diff --git a/src/math/j1.go b/src/math/j1.go
index cc19e75b95..7a30b4d973 100644
--- a/src/math/j1.go
+++ b/src/math/j1.go
@@ -132,6 +132,9 @@ func J1(x float64) float64 {
return z
}
if x < TwoM27 { // |x|<2**-27
+ if sign {
+ return -0.5 * x
+ }
return 0.5 * x // inexact if x!=0 necessary
}
z := x * x