math: Atan2 special cases and tests; atan2_386
Added special cases, tests and benchmarks for Atan2;
added 386 FPU version of Atan2.
R=rsc
CC=golang-dev
https://golang.org/cl/201068
diff --git a/src/pkg/math/atan2.go b/src/pkg/math/atan2.go
index 80a28b1..a738fbc 100644
--- a/src/pkg/math/atan2.go
+++ b/src/pkg/math/atan2.go
@@ -7,14 +7,69 @@
// Atan2 returns the arc tangent of y/x, using
// the signs of the two to determine the quadrant
// of the return value.
+//
+// Special cases are (in order):
+// Atan2(y, NaN) = NaN
+// Atan2(NaN, x) = NaN
+// Atan2(0, x>=0) = 0
+// Atan2(0, x<0) = Pi
+// Atan2(y>0, 0) = +Pi/2
+// Atan2(y<0, 0) = -Pi/2
+// Atan2(+Inf, +Inf) = +Pi/4
+// Atan2(-Inf, +Inf) = -Pi/4
+// Atan2(+Inf, -Inf) = 3Pi/4
+// Atan2(-Inf, -Inf) = -3Pi/4
+// Atan2(y, +Inf) = 0
+// Atan2(y>0, -Inf) = +Pi
+// Atan2(y<0, -Inf) = -Pi
+// Atan2(+Inf, x) = +Pi/2
+// Atan2(-Inf, x) = -Pi/2
func Atan2(y, x float64) float64 {
- // Determine the quadrant and call atan.
- if y+x == y {
- if y >= 0 {
+ // TODO(rsc): Remove manual inlining of IsNaN, IsInf
+ // when compiler does it for us
+ // special cases
+ switch {
+ case y != y || x != x: // IsNaN(y) || IsNaN(x):
+ return NaN()
+ case y == 0:
+ if x >= 0 {
+ return 0
+ }
+ return Pi
+ case x == 0:
+ if y > 0 {
+ return Pi / 2
+ }
+ return -Pi / 2
+ case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0):
+ if x > MaxFloat64 { // IsInf(x, 1) {
+ switch {
+ case y > MaxFloat64: // IsInf(y, 1):
+ return Pi / 4
+ case y < -MaxFloat64: // IsInf(y, -1):
+ return -Pi / 4
+ default:
+ return 0
+ }
+ }
+ switch {
+ case y > MaxFloat64: //IsInf(y, 1):
+ return 3 * Pi / 4
+ case y < -MaxFloat64: //IsInf(y, -1):
+ return -3 * Pi / 4
+ case y > 0:
+ return Pi
+ default:
+ return -Pi
+ }
+ case y < -MaxFloat64 || y > MaxFloat64: //IsInf(y, 0):
+ if y > MaxFloat64 { // IsInf(y, 1) {
return Pi / 2
}
return -Pi / 2
}
+
+ // Call atan and determine the quadrant.
q := Atan(y / x)
if x < 0 {
if q <= 0 {