Golang 查找给定数字的十进制指数
Go 语言提供了内置支持基本常数和数学函数,以便使用 math 包对数字执行操作。您可以使用 math 包提供的 pow10() 函数 查找指定数字的十进制指数 (10 ** a)。因此,您需要使用 import 关键字在程序中添加 math 包以访问 Pow10() 函数。
语法:
func Pow10(a int) float64
- 如果 a < -323,则此函数将返回 0。
- 如果 a > 308,则此函数将返回 +Inf。
示例 1:
// Golang 程序演示如何查找给定数字的十进制指数
package main
import (
"fmt"
"math"
)
// 主函数
func main() {
// 查找给定数字的十进制指数
// 使用 Pow10() 函数
res_1 := math.Pow10(3)
res_2 := math.Pow10(-2)
res_3 := math.Pow10(310)
res_4 := math.Pow10(-300)
// 显示结果
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)
}
输出:
Result 1: 1000.0
Result 2: 0.0
Result 3: +Inf
Result 4: 0.0
示例 2:
// Golang 程序演示如何查找给定数字的十进制指数
package main
import (
"fmt"
"math"
)
// 主函数
func main() {
// 查找给定数字的十进制指数
// 使用 Pow10() 函数
nvalue_1 := math.Pow10(2)
nvalue_2 := math.Pow10(3)
// 给定指数的总和
res := nvalue_1 + nvalue_2
fmt.Printf("%.2f + %.2f = %.2f",
nvalue_1, nvalue_2, res)
}
输出:
100.00 + 1000.00 = 1100.00