英文:
What is the equivalent for bigint.pow(a) in Go?
问题
在我的使用案例中,我想知道以下Java代码在Go中如何实现:
import (
"fmt"
"math/big"
)
func main() {
base := big.NewInt(16)
exponent := big.NewInt(1)
a := new(big.Int).Exp(base, exponent, nil)
fmt.Println(a) // 16
}
我可以导入math/big
包并创建大整数,但无法使用Go中的Pow()
函数。在Go的文档中也找不到该函数。
我需要为bigint实现自己的Pow()
版本吗?有人可以帮助我吗?
英文:
In my use case, I would like to know how the following Java code would be implemented in Go
BigInteger base = new BigInteger("16");
int exponent = 1;
BigInteger a = base.pow(exponent); //16^1 = 16
I am able to import the math/big
package and create big integers, but not able to do Pow()
function in Go. Also I don't find the function in the Go doc.
Do I have to implement my own version of Pow()
for bigint? Could anyone help me on this?
答案1
得分: 29
使用Exp
函数,将m
设置为nil
。
var i, e = big.NewInt(16), big.NewInt(2)
i.Exp(i, e, nil)
fmt.Println(i) // 输出 256
Playground: http://play.golang.org/p/0QFbNHEsn5
英文:
Use Exp
with m
set to nil
.
var i, e = big.NewInt(16), big.NewInt(2)
i.Exp(i, e, nil)
fmt.Println(i) // Prints 256
Playground: http://play.golang.org/p/0QFbNHEsn5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论