在 Golang 中查詢複數的餘弦值
餘弦函式是一個三角函式,用於確定直角三角形中鄰邊與斜邊之比。在處理複數時,餘弦函式用於確定複數的實部。
在 Golang 中,math/cmplx 包提供了各種用於複數的數學函式,包括餘弦函式。在本文中,我們將探討如何在 Golang 中查詢複數的餘弦值。
語法
在 Golang 中查詢複數餘弦值的語法如下:
cosine := cmplx.Cos(complexNumber)
其中:
Cosine 是複數的實部。
complexNumber 是要查詢其餘弦值的複數。
示例 1
讓我們使用 Golang 中的 cmplx.Cos() 函式查詢複數 (3+4i) 的餘弦值。
package main
import (
"fmt"
"math/cmplx"
)
func main() {
complexNumber := complex(3, 4)
cosine := cmplx.Cos(complexNumber)
fmt.Printf("The cosine of %v is %v", complexNumber, cosine)
}
輸出
The cosine of (3+4i) is (-27.034945603074224-3.851153334811777i)
示例 2
現在讓我們使用 cmplx.Cos() 函式查詢另一個複數 (1+2i) 的餘弦值。
package main
import (
"fmt"
"math/cmplx"
)
func main() {
complexNumber := complex(1, 2)
cosine := cmplx.Cos(complexNumber)
fmt.Printf("The cosine of %v is %v", complexNumber, cosine)
}
輸出
The cosine of (1+2i) is (2.0327230070196656-3.0518977991518i)
示例 3
讓我們使用 cmplx.Cos() 函式查詢另一個複數 (-2+3i) 的餘弦值。
package main
import (
"fmt"
"math/cmplx"
)
func main() {
complexNumber := complex(-2, 3)
cosine := cmplx.Cos(complexNumber)
fmt.Printf("The cosine of %v is %v", complexNumber, cosine)
}
輸出
The cosine of (-2+3i) is (-4.189625690968807+9.109227893755337i)
結論
複數的餘弦值是該複數的實部。在 Golang 中,我們可以使用 cmplx.Cos() 函式查詢複數的餘弦值。cmplx.Cos() 函式以複數作為輸入,並返回相應餘弦值的實部。我們可以使用 cmplx 包在 Golang 中對複數執行各種數學運算。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP