Home »
Golang »
Golang Reference
Golang math.Atan2() Function with Examples
Golang | math.Atan2() Function: Here, we are going to learn about the Atan2() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 29, 2021
math.Atan2()
The Atan2() function is an inbuilt function of the math package which is used to get the arctangent value (in radians) of y/x (where y is the first parameter and x is the second parameter) using the signs of the two to determine the quadrant of the return value.
It accepts two parameters and returns the arctangent (in radians) of y/x.
Syntax
func Atan2(y, x float64) float64
Parameters
- y, x : The values whose arctangent value is to be found.
Return Value
The return type of Atan2() function is a float64, it returns the arctangent value (in radians) of y/x.
Special cases are:
Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(+0, x>=0) = +0
Atan2(-0, x>=0) = -0
Atan2(+0, x<=-0) = +Pi
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
Example 1
// Golang program to demonstrate the
// example of math.Atan2() Function
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Atan2(0, 0))
fmt.Println(math.Atan2(1, 1))
fmt.Println(math.Atan2(2, math.NaN()))
fmt.Println(math.Atan2(math.NaN(), 2))
fmt.Println(math.Atan2(0, 1))
fmt.Println(math.Atan2(-0, 1))
fmt.Println(math.Atan2(0, -1.5))
fmt.Println(math.Atan2(-0, -1.5))
fmt.Println(math.Atan2(1, 0))
fmt.Println(math.Atan2(0.5, 0))
fmt.Println(math.Atan2(math.Inf(1), math.Inf(1)))
fmt.Println(math.Atan2(math.Inf(-1), math.Inf(1)))
fmt.Println(math.Atan2(math.Inf(1), math.Inf(-1)))
fmt.Println(math.Atan2(math.Inf(-1), math.Inf(-1)))
fmt.Println(math.Atan2(2, math.Inf(1)))
fmt.Println(math.Atan2(1, math.Inf(-1)))
fmt.Println(math.Atan2(0.5, math.Inf(-1)))
fmt.Println(math.Atan2(math.Inf(1), 2))
fmt.Println(math.Atan2(math.Inf(-1), 2))
}
Output:
0
0.7853981633974483
NaN
NaN
0
0
3.141592653589793
3.141592653589793
1.5707963267948966
1.5707963267948966
0.7853981633974483
-0.7853981633974483
2.356194490192345
-2.356194490192345
0
3.141592653589793
3.141592653589793
1.5707963267948966
-1.5707963267948966
Example 2
// Golang program to demonstrate the
// example of math.Atan2() Function
package main
import (
"fmt"
"math"
)
func main() {
var y float64
var x float64
var Atan2 float64
y = 0
x = 0
Atan2 = math.Atan2(y, x)
fmt.Println("Arctangent value of", y, "/", x, "is", Atan2)
y = 1
x = 2
Atan2 = math.Atan2(y, x)
fmt.Println("Arctangent value of", y, "/", x, "is", Atan2)
y = -1
x = 2.5
Atan2 = math.Atan2(y, x)
fmt.Println("Arctangent value of", y, "/", x, "is", Atan2)
}
Output:
Arctangent value of 0 / 0 is 0
Arctangent value of 1 / 2 is 0.4636476090008061
Arctangent value of -1 / 2.5 is -0.3805063771123649
Golang math Package Constants and Functions »