1
0
mirror of https://github.com/golang/go synced 2024-11-19 17:14:44 -07:00
go/src/pkg/math/atan2.go
Charles L. Dorian 072b560ee6 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
2010-02-05 14:55:19 -08:00

82 lines
1.7 KiB
Go

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package math
// 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 {
// 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 {
return q + Pi
}
return q - Pi
}
return q
}