Golang 查找指定数字的正切值
Go语言提供了基本常量和数学函数的内置支持,可借助math包对数字执行操作。您可以使用由math包提供的Tan()函数查找指定弧度参数的正切值。因此,您需要使用import关键字将math包添加到程序中以访问Tan()函数。
语法:
func Tan(y float64) float64
- 如果在此函数中传递了+Inf或-Inf,则此函数将返回NaN(非数字)。
- 如果在此函数中传递了NaN,则此函数将返回NaN。
- 如果在此函数中传递了0,则此函数将返回0。
示例1:
// Golang program to illustrate how to find
// the tangent of the given radian argument
package main
import (
"fmt"
"math"
)
// Main function
func main() {
// Finding the tangent
// of the given value
// Using Tan() function
res_1 := math.Tan(math.Pi / 4)
res_2 := math.Tan(math.Inf(-3))
res_3 := math.Tan(0)
res_4 := math.Tan(math.NaN())
res_5 := math.Tan(math.Pi)
// Displaying the result
fmt.Printf("Result 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: 1.0
Result 2: NaN
Result 3: 0.0
Result 4: NaN
Result 5: -0.0
示例2:
// Golang program to illustrate how to find
// the tangent of the given radian argument
package main
import (
"fmt"
"math"
)
// Main function
func main() {
// Finding tangent
// of the given number
nvalue_1 := math.Tan(math.Pi / 3)
nvalue_2 := math.Tan(math.Pi / 6)
// Sum of the given values
res := nvalue_1 + nvalue_2
// Displaying results
fmt.Printf("%.1f + %.1f = %.1f",
nvalue_1, nvalue_2, res)
}
输出:
1.7 + 0.6 = 2.3