From 5305bdd86bbe31d61c72e62f4e6729e3562ae178 Mon Sep 17 00:00:00 2001 From: Brian Kessler Date: Sat, 30 Dec 2017 01:27:28 -0700 Subject: [PATCH] =?utf8?q?math:=20correct=20result=20for=20Pow(x,=20=C2=B1?= =?utf8?q?.5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fixes #23224 The previous Pow code had an optimization for powers equal to ±0.5 that used Sqrt for increased accuracy/speed. This caused special cases involving powers of ±0.5 to disagree with the Pow spec. This change places the Sqrt optimization after all of the special case handling. Change-Id: I6bf757f6248256b29cc21725a84e27705d855369 Reviewed-on: https://go-review.googlesource.com/85660 Reviewed-by: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gobot Gobot --- src/math/all_test.go | 6 ++++++ src/math/pow.go | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/math/all_test.go b/src/math/all_test.go index 0412c19e57..6682395aa0 100644 --- a/src/math/all_test.go +++ b/src/math/all_test.go @@ -1589,6 +1589,7 @@ var vfpowSC = [][2]float64{ {Inf(-1), 1}, {Inf(-1), 3}, {Inf(-1), Pi}, + {Inf(-1), 0.5}, {Inf(-1), NaN()}, {-Pi, Inf(-1)}, @@ -1607,9 +1608,11 @@ var vfpowSC = [][2]float64{ {-1 / 2, Inf(1)}, {Copysign(0, -1), Inf(-1)}, {Copysign(0, -1), -Pi}, + {Copysign(0, -1), -0.5}, {Copysign(0, -1), -3}, {Copysign(0, -1), 3}, {Copysign(0, -1), Pi}, + {Copysign(0, -1), 0.5}, {Copysign(0, -1), Inf(1)}, {0, Inf(-1)}, @@ -1666,6 +1669,7 @@ var powSC = []float64{ Inf(-1), // pow(-Inf, 1) Inf(-1), // pow(-Inf, 3) Inf(1), // pow(-Inf, Pi) + Inf(1), // pow(-Inf, 0.5) NaN(), // pow(-Inf, NaN) 0, // pow(-Pi, -Inf) NaN(), // pow(-Pi, -Pi) @@ -1682,9 +1686,11 @@ var powSC = []float64{ 0, // pow(-1/2, +Inf) Inf(1), // pow(-0, -Inf) Inf(1), // pow(-0, -Pi) + Inf(1), // pow(-0, -0.5) Inf(-1), // pow(-0, -3) IEEE 754-2008 Copysign(0, -1), // pow(-0, 3) IEEE 754-2008 0, // pow(-0, +Pi) + 0, // pow(-0, 0.5) 0, // pow(-0, +Inf) Inf(1), // pow(+0, -Inf) Inf(1), // pow(+0, -Pi) diff --git a/src/math/pow.go b/src/math/pow.go index daebf94728..336193bce1 100644 --- a/src/math/pow.go +++ b/src/math/pow.go @@ -43,10 +43,6 @@ func pow(x, y float64) float64 { return 1 case y == 1: return x - case y == 0.5: - return Sqrt(x) - case y == -0.5: - return 1 / Sqrt(x) case IsNaN(x) || IsNaN(y): return NaN() case x == 0: @@ -81,6 +77,10 @@ func pow(x, y float64) float64 { case y > 0: return Inf(1) } + case y == 0.5: + return Sqrt(x) + case y == -0.5: + return 1 / Sqrt(x) } absy := y -- 2.50.0