dsfds656545
dsfds656545
2015-04-28 06:50

Go中的bigint.pow(a)等效于什么?

已采纳

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条回答

  • douao8204 douao8204 6年前

    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

    点赞 评论 复制链接分享

相关推荐