What is the Atan2 function in Golang?

The Atan2 function in the Go programming language is used to find the arc tangent value of the x/yx/y value, where xx and yy are the two parameters.

To use the Atan2 function, you must import the math package in your file and access the Atan2 function within it using the . notation (math.Atan2). Here, Atan2 is the actual function, while math is the Go package that stores the definition of this function.

Syntax

The definition of the Atan2 function inside the math package is shown below:

Parameters

The Atan2 function takes two arguments of type float64. The ratio of these two arguments (y/xy/x) is then used to calculate the value of the arc tangent.

Return value

The Atan2 function returns a single value of type float64 that represents the arc tangent value of the ratio of the arguments (y/xy/x). The possible range of all return values is between to π radians.

Below is a list of special cases in the return values:

  • NAN:

    • Atan2(y, NaN) = NaN
    • Atan2(NaN, x) = NaN
  • (+-)0:

    • Atan2(+0, x>=0) = +0
    • Atan2(-0, x>=0) = -0
    • Atan2(y, +Inf) = +0
  • (+-)π:

    • Atan2(+0, x<=-0) = +π
    • Atan2(-0, x<=-0) = -π
    • Atan2(y>0, -Inf) = +π
    • Atan2(y<0, -Inf) = -π
  • (+-)π/2:

    • Atan2(y>0, 0) = +π/2
    • Atan2(y<0, 0) = -π/2
    • Atan2(+Inf, x) = +π/2
    • Atan2(-Inf, x) = -π/2
  • (+-)π/4:

    • Atan2(+Inf, +Inf) = +π/4
    • Atan2(-Inf, +Inf) = -π/4
  • (+-)3π/4:

    • Atan2(+Inf, -Inf) = 3π/4
    • Atan2(-Inf, -Inf) = -3π/4

Examples

Below is a simple example in which we use the Atan2 function to find the arc tangent value with the parameters -25 and 25:

package main
import "fmt"
import "math"
func main() {
x := math.Atan2(-25, 25)
fmt.Println(x)
}

The following example shows that NAN is returned whenever there is NAN in any of the arguments:

package main
import "fmt"
import "math"
func main() {
x := math.Atan2(math.NaN(), 25)
fmt.Println(x)
y := math.Atan2(25, math.NaN())
fmt.Println(y)
}

Free Resources