Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to do modulo with bigInt using math/big in Golang?

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))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading