The Go big Int Cmp library is a package that provides big integer comparison functions in Go. It allows users to compare two big integers and determine whether they are equal, lesser, or greater than each other.
Examples:
1) Comparing two big integers:
package main
import ( "fmt" "math/big" )
func main() { a := new(big.Int).SetInt64(123456789) b := new(big.Int).SetInt64(987654321) cmp := a.Cmp(b) switch cmp { case -1: fmt.Println("a is less than b") case 0: fmt.Println("a is equal to b") case 1: fmt.Println("a is greater than b") } }
This example sets two big integers "a" and "b" with the values 123456789 and 987654321, respectively. It then compares them using the Cmp function and outputs the result using a switch statement. In this case, the output will be "a is less than b".
2) Checking if a big integer is zero:
package main
import ( "fmt" "math/big" )
func main() { a := new(big.Int).SetInt64(0) b := new(big.Int).SetInt64(12345) if a.Cmp(big.NewInt(0)) == 0 { fmt.Println("a is zero") } if b.Cmp(big.NewInt(0)) == 1 { fmt.Println("b is not zero") } }
This example sets two big integers "a" and "b". It then compares "a" to a new big integer with the value zero using the Cmp function and outputs "a is zero" if they are equal. It also compares "b" to the same big integer and outputs "b is not zero" if "b" is greater.
Package library: math/big
Golang Int.Cmp - 30 examples found. These are the top rated real world Golang examples of big.Int.Cmp extracted from open source projects. You can rate examples to help us improve the quality of examples.