The "math/big" package library in Go provides arbitrary-precision arithmetic functions for integers. One such function is "Div," which can be used to perform integer division.
Description:
The "Div" function divides one "big.Int" integer by another and returns the quotient and remainder as "big.Int" values. The returned "big.Int" quotient will be rounded towards zero.
Code Examples:
Example 1:
package main
import ( "fmt" "math/big" )
func main() { var x, y, q, r big.Int x.SetString("12345678901234567890", 10) // set x = 12345678901234567890 y.SetInt64(123) // set y = 123 q.Div(&x, &y) // perform x/y division and store quotient in q r.Mod(&x, &y) // perform x%y modulo and store remainder in r fmt.Printf("%v / %v = %v\n", &x, &y, &q) // prints: 12345678901234567890 / 123 = 100371032130081300 fmt.Printf("%v %% %v = %v\n", &x, &y, &r) // prints: 12345678901234567890 % 123 = 69 }
Example 2:
package main
import ( "fmt" "math/big" )
func main() { var x, y, q, r big.Int x.SetInt64(100) // set x = 100 y.SetInt64(7) // set y = 7 q.Div(&x, &y) // perform x/y division and store quotient in q r.Mod(&x, &y) // perform x%y modulo and store remainder in r fmt.Printf("%v / %v = %v\n", &x, &y, &q) // prints: 100 / 7 = 14 fmt.Printf("%v %% %v = %v\n", &x, &y, &r) // prints: 100 % 7 = 2 }
In both examples, we use the "big.Int" type to set up the input values for the division operation, and we use the "Div" and "Mod" functions to determine the quotient and remainder respectively. We then print the results using the "printf" function.
Package Library:
The "math/big" package library is a standard Go library, and is included in the standard distribution of Go. It provides arbitrary-precision arithmetic for integers and floats, as well as useful mathematical functions like square roots, logarithms, and trigonometric functions.
Golang Int.Div - 30 examples found. These are the top rated real world Golang examples of math/big.Int.Div extracted from open source projects. You can rate examples to help us improve the quality of examples.