Home > Software design >  what is the mod operator % for bigInt in golang
what is the mod operator % for bigInt in golang

Time:11-07

I am using the package math/big and cannot find the operator % for bigInt.

Do I need to implement the operad in a function?

And how to convert bigInt to bigFloat?

CodePudding user response:

If what you want is the equivalent to Go's % operator, then you have to use Rem or QuoRem.

Rem implements truncated modulus (like Go)

Mod and DivMod implement Euclidean modulus.

package main

import (
    "fmt"
    "math/big"
)

func main() {

    x := int64(-40)
    y := int64(-12)

    fmt.Println(x % y) // -4

    _, mod := new(big.Int).DivMod(big.NewInt(x), big.NewInt(y), new(big.Int))
    fmt.Println(mod) // 8

    _, rem := new(big.Int).QuoRem(big.NewInt(x), big.NewInt(y), new(big.Int))
    fmt.Println(rem) // -4
}

CodePudding user response:

Use 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. If y == 0, a division-by-zero run-time panic occurs.

  •  Tags:  
  • go
  • Related