Commit bd986286 authored by Bryan C. Mills's avatar Bryan C. Mills

math/cmplx: avoid panic in Pow(x, NaN())

Fixes #30088

Change-Id: I08cec17feddc86bd08532e6b135807e3c8f4c1b2
Reviewed-on: https://go-review.googlesource.com/c/161197
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 4a9aba5a
...@@ -400,9 +400,11 @@ var polarSC = []ff{ ...@@ -400,9 +400,11 @@ var polarSC = []ff{
} }
var vcPowSC = [][2]complex128{ var vcPowSC = [][2]complex128{
{NaN(), NaN()}, {NaN(), NaN()},
{0, NaN()},
} }
var powSC = []complex128{ var powSC = []complex128{
NaN(), NaN(),
NaN(),
} }
var vcSinSC = []complex128{ var vcSinSC = []complex128{
NaN(), NaN(),
...@@ -734,8 +736,8 @@ func TestPow(t *testing.T) { ...@@ -734,8 +736,8 @@ func TestPow(t *testing.T) {
} }
} }
for i := 0; i < len(vcPowSC); i++ { for i := 0; i < len(vcPowSC); i++ {
if f := Pow(vcPowSC[i][0], vcPowSC[i][0]); !cAlike(powSC[i], f) { if f := Pow(vcPowSC[i][0], vcPowSC[i][1]); !cAlike(powSC[i], f) {
t.Errorf("Pow(%g, %g) = %g, want %g", vcPowSC[i][0], vcPowSC[i][0], f, powSC[i]) t.Errorf("Pow(%g, %g) = %g, want %g", vcPowSC[i][0], vcPowSC[i][1], f, powSC[i])
} }
} }
for _, pt := range branchPoints { for _, pt := range branchPoints {
......
...@@ -48,6 +48,9 @@ import "math" ...@@ -48,6 +48,9 @@ import "math"
// Pow(0, c) for real(c)<0 returns Inf+0i if imag(c) is zero, otherwise Inf+Inf i. // Pow(0, c) for real(c)<0 returns Inf+0i if imag(c) is zero, otherwise Inf+Inf i.
func Pow(x, y complex128) complex128 { func Pow(x, y complex128) complex128 {
if x == 0 { // Guaranteed also true for x == -0. if x == 0 { // Guaranteed also true for x == -0.
if IsNaN(y) {
return NaN()
}
r, i := real(y), imag(y) r, i := real(y), imag(y)
switch { switch {
case r == 0: case r == 0:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment