gitee.com/quant1x/num@v0.3.2/math32/atan2.go (about)

     1  package math32
     2  
     3  // Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.
     4  // Special cases are (in order):
     5  //
     6  //	Atan2(y, NaN) = NaN
     7  //	Atan2(NaN, x) = NaN
     8  //	Atan2(+0, x>=0) = +0
     9  //	Atan2(-0, x>=0) = -0
    10  //	Atan2(+0, x<=-0) = +Pi
    11  //	Atan2(-0, x<=-0) = -Pi
    12  //	Atan2(y>0, 0) = +Pi/2
    13  //	Atan2(y<0, 0) = -Pi/2
    14  //	Atan2(+Inf, +Inf) = +Pi/4
    15  //	Atan2(-Inf, +Inf) = -Pi/4
    16  //	Atan2(+Inf, -Inf) = 3Pi/4
    17  //	Atan2(-Inf, -Inf) = -3Pi/4
    18  //	Atan2(y, +Inf) = 0
    19  //	Atan2(y>0, -Inf) = +Pi
    20  //	Atan2(y<0, -Inf) = -Pi
    21  //	Atan2(+Inf, x) = +Pi/2
    22  //	Atan2(-Inf, x) = -Pi/2
    23  func Atan2(y, x float32) float32 {
    24  	// special cases
    25  	switch {
    26  	case IsNaN(y) || IsNaN(x):
    27  		return NaN()
    28  	case y == 0:
    29  		if x >= 0 && !Signbit(x) {
    30  			return Copysign(0, y)
    31  		}
    32  		return Copysign(Pi, y)
    33  	case x == 0:
    34  		return Copysign(Pi/2, y)
    35  	case IsInf(x, 0):
    36  		if IsInf(x, 1) {
    37  			switch {
    38  			case IsInf(y, 0):
    39  				return Copysign(Pi/4, y)
    40  			default:
    41  				return Copysign(0, y)
    42  			}
    43  		}
    44  		switch {
    45  		case IsInf(y, 0):
    46  			return Copysign(3*Pi/4, y)
    47  		default:
    48  			return Copysign(Pi, y)
    49  		}
    50  	case IsInf(y, 0):
    51  		return Copysign(Pi/2, y)
    52  	}
    53  
    54  	// Call atan and determine the quadrant.
    55  	q := Atan(y / x)
    56  	if x < 0 {
    57  		if q <= 0 {
    58  			return q + Pi
    59  		}
    60  		return q - Pi
    61  	}
    62  	return q
    63  }