aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Chase <drchase@google.com>2019-03-13 16:46:10 -0400
committerDavid Chase <drchase@google.com>2019-03-15 14:52:51 +0000
commit653579138555ff2728ba16f841b640e06deab8df (patch)
tree5f87491cc7412f08e21877a33d177f4646a1931f /src
parent9d9d4eeb87ed5dae09e26d744ea7d6caf9b182dc (diff)
downloadgo-653579138555ff2728ba16f841b640e06deab8df.tar.xz
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 <drchase@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/math/all_test.go14
-rw-r--r--src/math/remainder.go4
2 files changed, 18 insertions, 0 deletions
diff --git a/src/math/all_test.go b/src/math/all_test.go
index ed42941780..208c8233e0 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 504fdda7df..7c77d6eb3b 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 {