Complete Guide to the math and math/big Packages in Go

Use the math package for standard calculations and math/big for arbitrary-precision arithmetic with very large numbers.

The math package provides standard mathematical functions like Sqrt and Sin, while math/big handles arbitrary-precision arithmetic for integers and floats that exceed standard limits. Use math/big when you need exact results for very large numbers or high-precision decimals.

package main

import (
	"fmt"
	"math"
	"math/big"
)

func main() {
	// Standard math
	fmt.Println(math.Sqrt(2))

	// Arbitrary precision
	n := new(big.Int)
	n.SetString("123456789012345678901234567890", 10)
	fmt.Println(n)
}