Open In App

math.Atan2() Function in Golang With Examples

Last Updated : 13 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You can find the arc tangent of a/b, using the signs of the two to identify the quadrant of the return value with the help of Atan2() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access the Atan2() function. Syntax:
func Atan2(b, a float64) float64
  • If Atan2(b, NaN), then this method will return NaN.
  • If Atan2(NaN, a), then this method will return NaN.
  • If Atan2(+0, a>=0), then this method will return +0.
  • If Atan2(-0, a>=0), then this method will return -0.
  • If Atan2(+0, a<=0), then this method will return +Pi.
  • If Atan2(-0, a<=0), then this method will return -Pi.
  • If Atan2(b>0, 0), then this method will return +Pi/2.
  • If Atan2(b<0, 0), then this method will return -Pi/2.
  • If Atan2(+Inf, +Inf), then this method will return +Pi/4.
  • If Atan2(-Inf, +Inf), then this method will return -Pi/4.
  • If Atan2(+Inf, -Inf), then this method will return -3Pi/4.
  • If Atan2(+Inf, +Inf), then this method will return 0.
  • If Atan2(b, +Inf), then this method will return +Pi/4.
  • If Atan2(b>0, -Inf), then this method will return +Pi.
  • If Atan2(b<0, -Inf), then this method will return -Pi.
  • If Atan2(+Inf, a), then this method will return +Pi/2.
  • If Atan2(-Inf, a), then this method will return -Pi/2.
Example 1: C
// Golang program to illustrate 
// the math.Atan2() function
package main

import (
    "fmt"
    "math"
)

// Main function
func main() {

    // Finding arc tangent
    // Using Atan2() function
    res_1 := math.Atan2(math.Pi/3, math.Pi/4)
    res_2 := math.Atan2(math.Inf(1), 0)
    res_3 := math.Atan2(math.Inf(-2), math.Inf(+2))
    res_4 := math.Atan2(1, math.NaN())
    res_5 := math.Atan2(math.NaN(), 0)

    // Displaying the result
    fmt.Printf("\nResult 1: %.1f", res_1)
    fmt.Printf("\nResult 2: %.1f", res_2)
    fmt.Printf("\nResult 3: %.1f", res_3)
    fmt.Printf("\nResult 4: %.1f", res_4)
    fmt.Printf("\nResult 5: %.1f", res_5)
}
Output:
Result 1: 0.9
Result 2: 1.6
Result 3: -0.8
Result 4: NaN
Result 5: NaN
Example 2: C
// Golang program to illustrate
// the math.Atan2() function
package main

import (
    "fmt"
    "math"
)

// Main function
func main() {

    // Finding arc tangent
    // Using Atan2() function
    nvalue_1 := math.Atan2(math.Pi/3, math.Pi/4)
    nvalue_2 := math.Atan2(math.Pi/6, math.Pi/3)

    // Sum of the given numbers
    res := nvalue_1 + nvalue_2
    fmt.Printf("%.2f + %.2f = %.2f",
            nvalue_1, nvalue_2, res)

}
Output:
0.93 + 0.46 = 1.39

Next Article
Article Tags :

Similar Reads