當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang math.Atan2()用法及代碼示例

Go語言為基本常數和數學函數提供了內置支持,以借助數學包對數字進行運算。您可以找到a /b的反正切值,並使用數學包提供的Atan2()函數,使用兩個符號來確定返回值的象限。因此,您需要借助import關鍵字在程序中添加數學包,以訪問Atan2()函數。

用法:

func Atan2(b, a float64) float64
  • 如果為Atan2(b,NaN),則此方法將返回NaN。
  • 如果為Atan2(NaN,a),則此方法將返回NaN。
  • 如果Atan2(+0,a> = 0),則此方法將返回+0。
  • 如果Atan2(-0,a> = 0),則此方法將返回-0。
  • 如果Atan2(+0,a <= 0),則此方法將返回+ Pi。
  • 如果Atan2(-0,a <= 0),則此方法將返回-Pi。
  • 如果Atan2(b> 0,0),則此方法將返回+ Pi /2。
  • 如果Atan2(b <0,0),則此方法將返回-Pi /2。
  • 如果為Atan2(+ Inf,+ Inf),則此方法將返回+ Pi /4。
  • 如果為Atan2(-Inf,+ Inf),則此方法將返回-Pi /4。
  • 如果為Atan2(+ Inf,-Inf),則此方法將返回-3Pi /4。
  • 如果Atan2(+ Inf,+ Inf),則此方法將返回0。
  • 如果為Atan2(b,+ Inf),則此方法將返回+ Pi /4。
  • 如果Atan2(b> 0,-Inf),則此方法將返回+ Pi。
  • 如果Atan2(b <0,-Inf),則此方法將返回-Pi。
  • 如果為Atan2(+ Inf,a),則此方法將返回+ Pi /2。
  • 如果為Atan2(-Inf,a),則此方法將返回-Pi /2。

範例1:

// 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) 
}

輸出:

Result 1:0.9
Result 2:1.6
Result 3:-0.8
Result 4:NaN
Result 5:NaN

範例2:

// 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) 
  
}

輸出:

0.93 + 0.46 = 1.39



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 math.Atan2() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。