如何在Go語言中查詢給定弧度值的正切?


給定弧度的正切是指其鄰邊與對邊的比率。Go語言擁有許多包含預定義函式的包,開發者可以使用這些函式而無需編寫完整的邏輯。為了執行數學運算和邏輯,Go語言中有一個math包。我們將僅使用此包來查詢給定弧度值的正切。我們還將瞭解如何匯入該包以及如何透過編寫Go程式碼來呼叫該包中包含的函式。

正切

定義

正切是三角函式的一部分。要理解正切,請觀察下圖。

如果我們藉助上圖定義正切,則角度$\theta$的正切函式等於對邊與鄰邊的比率。


tan$\theta$ = 對邊 / 鄰邊

不同角度下的正切值

  • tan(0) = 0

  • tan(30) = 1 / √3

  • tan(45) = 1

  • tan(60) = √3

  • tan(90) = 未定義

  • tan(120) = − √3

  • tan(135) = − 1

  • tan(150) = −1 / √3

  • tan(180) = 0

圖形

現在我們將看到正切函式的圖形,並在圖形上觀察上述值。對於零角度,值為零,然後直到角度達到90度,值為未定義。然後,直到180度,我們將得到第四象限的映象。

演算法

步驟 1 − 宣告變數以儲存弧度值和float32型別的答案。

步驟 2 − 初始化弧度變數。

步驟 3 − 呼叫正切函式並傳遞弧度值。

步驟 4 − 列印結果。

示例 1

在這個例子中,我們將編寫一個Go程式,在其中我們將匯入math包並呼叫正切函式。

package main

import (
    // fmt package provides the function to print anything
    "fmt"

    // math package provides multiple functions for different
    // mathematical operations
    "math"
)

func main() {
    // declaring the variables to store the value of radian value and answer
    var radianValue, answer float64

    fmt.Println("Program to find the Tangent of a given radian value in the Golang programming language using a math package.")

    // initializing the value of the radian value
    radianValue = 4.5

     // finding tangent for the given radian value 
     answer = math.Tan(radianValue)

    // printing the result
    fmt.Println("The Tangent value with the value of radian", radianValue, "is", answer)
}

輸出

Program to find the Tangent of a given radian value in the Golang programming language using a math package.
The Tangent value with the value of radian 4.5 is 4.637332054551185

示例 2

在這個例子中,我們將編寫一個Go程式,在其中我們將匯入math包,在一個單獨的函式中呼叫正切函式,並在主函式中呼叫該函式。

package main

import (
    // fmt package provides the function to print anything
    "fmt"

    // math package provides multiple functions for different
    // mathematical operations
    "math"
)

// this is a function with a parameter of float64 type and a return type of float64
func Tangent(angle float64) float64 {
    // returning the Tangent of the angle
    return math.Tan(angle)
}

func main() {
    // declaring the variables to store the value of radian value and answer
    var radianValue, answer float64

    fmt.Println("Program to find the Tangent of a given radian value in the Golang programming language using a separate function in the same program.")

    // initializing the value of the radian value
    radianValue = 4.5

    // finding factorial of n
    answer = Tangent(radianValue)

     // finding tangent for the given radian value in the separate function
    fmt.Println("The Tangent value with the value of radian", radianValue, "is", answer)
}

輸出

Program to find the Tangent of a given radian value in the Golang programming language using a separate function in the same program.
The Tangent value with the value of radian 4.5 is 4.637332054551185

結論

這兩種方法都是透過使用math包中的函式並將弧度值作為引數傳遞來查詢正切。如果我們比較這兩種方法,則建立單獨函式的第二種方法將透過建立單獨的函式來在程式中提供抽象,並且可以在不同的地方重複使用。要了解更多關於Go語言的資訊,您可以瀏覽這些教程

更新於:2023年7月10日

62 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.