What is the equivalent for bigint.pow(a) in Go?

huangapple go评论85阅读模式
英文:

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

huangapple
  • 本文由 发表于 2015年4月28日 14:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/29912249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定