Golang 找到给定数字的Base-e指数
Go语言提供内置的常量和数学函数,用于借助math包对数字执行操作。您可以使用由math包提供的Exp()函数找到基于e的指数,即 e ** a 特定数字的。因此,您需要使用导入关键字在程序中添加math包,以访问Exp()函数。
语法:
func Exp(a float64) float64
- 如果在此函数中传递+Inf,则此函数将返回+Inf。
- 在此函数中,非常大的值会溢出到0或+Inf,非常小的值会下溢到1。
- 如果在此函数中传递NaN,则此函数将返回NaN。
示例1:
// Golang程序展示如何
//找到给定数字的指数
package main
import (
"fmt"
"math"
)
//主函数
func main() {
//查找给定数字的基于e的指数
//使用Exp()函数
res_1 := math.Exp(3)
res_2 := math.Exp(1)
res_3 := math.Exp(math.Inf(1))
res_4 := math.Exp(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:20.1
结果2:2.7
结果3:+Inf
结果4:NaN
示例2:
// Golang程序展示如何
//查找给定数字的指数
package main
import (
"fmt"
"math"
)
//主函数
func main() {
//使用Exp()函数查找基于e的指数
nvalue_1 := math.Exp(2)
nvalue_2 := math.Exp(3)
res := nvalue_1 + nvalue_2
fmt.Printf("%.1f + %.1f = %.1f",
nvalue_1, nvalue_2, res)
}
输出:
7.4 + 20.1 = 27.5