Search code examples
gobigintegerbigintinequalityinequalities

Comparing inequalities for Go bigInt?


I am trying to compare two big ints. I'm looking at the docs: https://pkg.go.dev/math/big#Int and I don't see an inequality operator, but I do see an equality one (Cmp).

How am I meant to compare a big int a to a big int b? Am I meant to subtract b from a and compare the sign? Or is there something I am missing? E.g.

c := (new.bigInt).Sub(a, b)
i := c.Sign()
if i < 0 {
    fmt.Println("a < b")
}

It seems a little odd to me there is not an inequality operator, nor much about this online so I think I am doing something wrong.


Solution

  • Cmp returns: -1 if x < y, 0 if x == y, +1 if x > y

    Go Playground Example