Skip to content

Commit 6535791

Browse files
committed
math: fix math.Remainder(-x,x) (for Inf > x > 0)
Modify the |x| == |y| case to return -0 when x < 0. Fixes #30814. Change-Id: Ic4cd48001e0e894a12b5b813c6a1ddc3a055610b Reviewed-on: https://go-review.googlesource.com/c/go/+/167479 Run-TryBot: David Chase <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 9d9d4ee commit 6535791

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/math/all_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,6 +2795,20 @@ func TestRemainder(t *testing.T) {
27952795
if f := Remainder(5.9790119248836734e+200, 1.1258465975523544); -0.4810497673014966 != f {
27962796
t.Errorf("Remainder(5.9790119248836734e+200, 1.1258465975523544) = %g, want -0.4810497673014966", f)
27972797
}
2798+
// verify that sign is correct when r == 0.
2799+
test := func(x, y float64) {
2800+
if r := Remainder(x, y); r == 0 && Signbit(r) != Signbit(x) {
2801+
t.Errorf("Remainder(x=%f, y=%f) = %f, sign of (zero) result should agree with sign of x", x, y, r)
2802+
}
2803+
}
2804+
for x := 0.0; x <= 3.0; x += 1 {
2805+
for y := 1.0; y <= 3.0; y += 1 {
2806+
test(x, y)
2807+
test(x, -y)
2808+
test(-x, y)
2809+
test(-x, -y)
2810+
}
2811+
}
27982812
}
27992813

28002814
func TestRound(t *testing.T) {

src/math/remainder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func remainder(x, y float64) float64 {
5757
y = -y
5858
}
5959
if x == y {
60+
if sign {
61+
zero := 0.0
62+
return -zero
63+
}
6064
return 0
6165
}
6266
if y <= HalfMax {

0 commit comments

Comments
 (0)