diff --git a/src/math/all_test.go b/src/math/all_test.go index ed42941780973a27b2f747757809c02fcada849f..208c8233e0d22e50b8f9a64be69e1fad0f09a6d1 100644 --- a/src/math/all_test.go +++ b/src/math/all_test.go @@ -2795,6 +2795,20 @@ func TestRemainder(t *testing.T) { if f := Remainder(5.9790119248836734e+200, 1.1258465975523544); -0.4810497673014966 != f { t.Errorf("Remainder(5.9790119248836734e+200, 1.1258465975523544) = %g, want -0.4810497673014966", f) } + // verify that sign is correct when r == 0. + test := func(x, y float64) { + if r := Remainder(x, y); r == 0 && Signbit(r) != Signbit(x) { + t.Errorf("Remainder(x=%f, y=%f) = %f, sign of (zero) result should agree with sign of x", x, y, r) + } + } + for x := 0.0; x <= 3.0; x += 1 { + for y := 1.0; y <= 3.0; y += 1 { + test(x, y) + test(x, -y) + test(-x, y) + test(-x, -y) + } + } } func TestRound(t *testing.T) { diff --git a/src/math/remainder.go b/src/math/remainder.go index 504fdda7dff64ea4237b784f9537a634aa100be7..7c77d6eb3b9edf879162c5fd512c8f0abe3a3e26 100644 --- a/src/math/remainder.go +++ b/src/math/remainder.go @@ -57,6 +57,10 @@ func remainder(x, y float64) float64 { y = -y } if x == y { + if sign { + zero := 0.0 + return -zero + } return 0 } if y <= HalfMax {