github.com/primecitizens/pcz/std@v0.2.1/math/atan2.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math
     6  
     7  // Atan2 returns the arc tangent of y/x, using
     8  // the signs of the two to determine the quadrant
     9  // of the return value.
    10  //
    11  // Special cases are (in order):
    12  //
    13  //	Atan2(y, NaN) = NaN
    14  //	Atan2(NaN, x) = NaN
    15  //	Atan2(+0, x>=0) = +0
    16  //	Atan2(-0, x>=0) = -0
    17  //	Atan2(+0, x<=-0) = +Pi
    18  //	Atan2(-0, x<=-0) = -Pi
    19  //	Atan2(y>0, 0) = +Pi/2
    20  //	Atan2(y<0, 0) = -Pi/2
    21  //	Atan2(+Inf, +Inf) = +Pi/4
    22  //	Atan2(-Inf, +Inf) = -Pi/4
    23  //	Atan2(+Inf, -Inf) = 3Pi/4
    24  //	Atan2(-Inf, -Inf) = -3Pi/4
    25  //	Atan2(y, +Inf) = 0
    26  //	Atan2(y>0, -Inf) = +Pi
    27  //	Atan2(y<0, -Inf) = -Pi
    28  //	Atan2(+Inf, x) = +Pi/2
    29  //	Atan2(-Inf, x) = -Pi/2
    30  func Atan2(y, x float64) float64 {
    31  	return atan2(y, x)
    32  }
    33  
    34  func atan2(y, x float64) float64 {
    35  	// special cases
    36  	switch {
    37  	case IsNaN(y) || IsNaN(x):
    38  		return NaN()
    39  	case y == 0:
    40  		if x >= 0 && !Signbit(x) {
    41  			return Copysign(0, y)
    42  		}
    43  		return Copysign(Pi, y)
    44  	case x == 0:
    45  		return Copysign(Pi/2, y)
    46  	case IsInf(x, 0):
    47  		if IsInf(x, 1) {
    48  			switch {
    49  			case IsInf(y, 0):
    50  				return Copysign(Pi/4, y)
    51  			default:
    52  				return Copysign(0, y)
    53  			}
    54  		}
    55  		switch {
    56  		case IsInf(y, 0):
    57  			return Copysign(3*Pi/4, y)
    58  		default:
    59  			return Copysign(Pi, y)
    60  		}
    61  	case IsInf(y, 0):
    62  		return Copysign(Pi/2, y)
    63  	}
    64  
    65  	// Call atan and determine the quadrant.
    66  	q := Atan(y / x)
    67  	if x < 0 {
    68  		if q <= 0 {
    69  			return q + Pi
    70  		}
    71  		return q - Pi
    72  	}
    73  	return q
    74  }