计算一个数字x^y在GO中的幂数的方法

122 阅读1分钟

要在GO中把一个数字X提高到Y的幂,请使用 Pow(x, y float64)函数从 math包中的函数。

package main
import (
"fmt"
"math"
)
func main() {
res := math.Pow(2, 7) // 2^7
 fmt.Println(res)
}

输出

128

要在Go中计算10的幂到Y的指数,请使用 math.Pow10(n int)函数。

package main
import (
"fmt"
"math"
)
func main() {
res := math.Pow10(3) // 10^3
 fmt.Println(res)
}

输出

1000