英文:
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个值,x、y和m。该方法计算x / y,并将结果设置为接收器z。除法的余数设置为第三个参数:m。该函数还返回接收器z和m。
例如:
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 = 3和m = 45,因为345 / 100 = 3,余数为45(345 = 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论