How to do modulo with bigInt using math/big?

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

How to do modulo with bigInt using math/big?

问题

我放了两个东西,但它需要三个,我不确定第三个输入应该放什么,为什么它返回2?z和m?我只需要一个输出。

z, m := new(big.Int).DivMod(big.NewInt(100), big.NewInt(1024))
if err != nil {
     glog.Info("%v", err)
}
bytePos := (m / big.NewInt(8))

我放了两个东西,但它需要三个,我不确定第三个输入应该放什么,为什么它返回2?z和m?我只需要一个输出。

英文:

I put 2 things, however it wants 3, i'm not sure what to put for third input and why is it returning 2? z and m? I just need one output.

z, m := new(big.Int).DivMod(big.NewInt(100), big.NewInt(1024))
if err != nil {
     glog.Info("%v", err)
					}
bytePos := (m / big.NewInt(8))

答案1

得分: 3

Int.DivMod()的文档:

func (z *Int) DivMod(x, y, m *Int) (*Int, *Int)

DivMod将z设置为商x除以y,将m设置为模x除以y,并返回(y != 0)的一对(z, m)。

你需要传递3个值,xym。该方法计算x / y,并将结果设置为接收器z。除法的余数设置为第三个参数:m。该函数还返回接收器zm

例如:

z, m := new(big.Int).DivMod(big.NewInt(345), big.NewInt(100), new(big.Int))

fmt.Println("z:", z)
fmt.Println("m:", m)

输出结果(在Go Playground上尝试):

z: 3
m: 45

结果是z = 3m = 45,因为345 / 100 = 3,余数为45345 = 3*100 + 45)。

英文:

Doc of Int.DivMod():

> func (z *Int) DivMod(x, y, m *Int) (*Int, *Int)
> DivMod sets z to the quotient x div y and m to the modulus x mod y and returns the pair (z, m) for y != 0.

You have to pass 3 values, x, y, and m. The method calculates x / y, and the result is set to the receiver z. The remainder of the division is set to the 3rd param: m. The function also returns the receiver z and m.

For example:

z, m := new(big.Int).DivMod(big.NewInt(345), big.NewInt(100), new(big.Int))

fmt.Println("z:", z)
fmt.Println("m:", m)

Output (try it on the Go Playground):

z: 3
m: 45

The result is z = 3 and m = 45 because 345 / 100 = 3 and the remainder is 45 (345 = 3*100 + 45).

huangapple
  • 本文由 发表于 2022年8月15日 17:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/73358960.html
匿名

发表评论

匿名网友

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

确定