Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Time, Math and Randomness

Go math/big package

Reference for Go math/big: Int, Rat and Float types for arbitrary precision arithmetic, the receiver-based API, parsing, comparing and primality tests.

The package math/big provides numbers with no fixed size limit. big.Int holds integers of any length, big.Rat holds exact fractions, and big.Float holds floating-point numbers with a precision you choose. Use them for factorials, cryptography, exact money arithmetic with fractions, and anywhere int64 or float64 would overflow or round.

The API looks unusual at first. Operations are methods that store the result in their receiver and return it: z.Add(x, y) sets z = x + y. That avoids allocating a new number for every step and lets you reuse variables, but it means you must create the receiver before you call the method.

Go
a := big.NewInt(2)
b := new(big.Int).Exp(a, big.NewInt(100), nil)
fmt.Println(b) // 1267650600228229401496703205376

Try it

Go big.Int: factorial and powers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go big.Rat and big.Float

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func NewInt(x int64) *Int
func (z *Int) Add(x, y *Int) *Int      // also Sub, Mul, Quo, Rem, Div, Mod, Exp, GCD
func (z *Int) SetString(s string, base int) (*Int, bool)
func (x *Int) Cmp(y *Int) int
func NewRat(a, b int64) *Rat
func NewFloat(x float64) *Float
Type Use Zero value
big.Int Arbitrary-size integers Valid, equal to 0
big.Rat Exact fractions a/b Valid, equal to 0
big.Float Floating point with settable precision (SetPrec, in bits) and rounding mode Valid, equal to 0 with precision set by the first operation

Errors and panics: division by zero panics (Quo, Rem, Div, Mod, NewRat(x, 0)). Operations on a Float NaN panic with ErrNaN. SetString reports failure with its boolean result. Exp(x, y, m) with a nil modulus computes plain x**y; with a non-nil m it computes x**y mod |m|.

Usage notes

  • Pass pointers, mutate receivers. x.Add(x, y) is allowed and updates x in place. The result and operands may be the same object.
  • Do not copy values. Copying a big.Int struct shares its internal storage; use new(big.Int).Set(x) for a real copy.
  • Quo/Rem truncate toward zero like Go's / and %; Div/Mod use Euclidean division, so the remainder is never negative.
  • Printing: fmt knows the types, so Println, %v, %d and %s work on *big.Int. Pass a pointer, not a value.
  • Conversions: Int64() and Uint64() silently wrap if the value does not fit; check with IsInt64() first. Float.Int64() truncates.
  • Speed: each operation allocates and is far slower than native arithmetic. Multiplication uses Karatsuba for large operands. Use int64 while values fit.
  • Primality: ProbablyPrime(n) runs Miller-Rabin with n rounds plus a Baillie-PSW test, so it is exact for inputs below 2^64 and reliable beyond.
  • Random primes and secrets belong to crypto/rand (rand.Prime, rand.Int), not to math/rand.