Home > Software design >  How to do modulo with bigInt using math/big?
How to do modulo with bigInt using math/big?

Time:08-16

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

CodePudding user response:

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

  •  Tags:  
  • go
  • Related