Golang 找到指定数的反正切函数
Go语言提供了基本常量和数学函数的内置支持,通过math包的帮助对数字进行运算。您可以使用由math包提供的Atan()函数找到特定数字的反正切值。因此,您需要使用import关键字在程序中添加math包来调用Atan()函数。
语法:
func Atan(y float64) float64
- 如果您在此函数中传递+Inf或-Inf,则此函数将返回+Pi/2或-Pi/2。
- 如果您在此函数中传递0,则该函数将返回0。
示例 1:
// Golang程序,展示如何找到给定数字的反正切值。
package main
import (
"fmt"
"math"
)
// 主函数
func main() {
// 找到给定值的反正切值
res_1 := math.Atan(1)
res_2 := math.Atan(math.Inf(-3))
res_3 := math.Atan(0)
res_4 := math.Atan(math.NaN())
// 显示结果
fmt.Printf("结果1:%.1f", res_1)
fmt.Printf("\n结果2:%.1f", res_2)
fmt.Printf("\n结果3:%.1f", res_3)
fmt.Printf("\n结果4:%.1f", res_4)
}
输出:
结果1:0.8
结果2:-1.6
结果3:0.0
结果4:NaN
示例 2:
// Golang程序,展示如何找到给定数字的反正切值。
package main
import (
"fmt"
"math"
)
// 主函数
func main() {
// 找到给定值的反正切值
nvalue_1 := math.Atan(1)
nvalue_2 := math.Atan(-3)
// 给定值的总和
res := nvalue_1 + nvalue_2
// 显示结果
fmt.Printf("%.1f + %.1f = %.1f", nvalue_1, nvalue_2, res)
}
输出:
0.8 + -1.2 = -0.5