Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Time, Math and Randomness

Go math package

Reference for the Go math package: constants, Abs, Max, Min, Mod, Trunc, logarithms, trigonometry, Inf and NaN handling, with runnable examples.

The package math holds the floating-point functions and constants that the language itself does not provide: square roots and powers, logarithms and exponentials, trigonometry, rounding, and the limits of the numeric types. Almost every function takes and returns float64; there are no integer overloads.

Float arithmetic follows IEEE 754, so there are special values: positive and negative infinity and NaN ("not a number"). Dividing a float by zero does not panic, it produces +Inf or -Inf, and 0.0/0.0 produces NaN. The package gives you tools to produce and test these values.

Go
fmt.Println(math.Sqrt(16), math.Pi)

Try it

Go Common functions and constants

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 Infinity and NaN

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

The most used members, grouped:

Group Members
Constants Pi, E, Sqrt2, Ln2, Phi, MaxInt, MinInt, MaxInt8...MaxInt64, MaxUint8...MaxUint64, MaxFloat32, MaxFloat64, SmallestNonzeroFloat64
Basic Abs, Max, Min, Mod, Remainder, Modf, Copysign, Dim
Powers and roots Sqrt, Cbrt, Pow, Pow10, Hypot, Exp, Exp2, Expm1
Logarithms Log (natural), Log2, Log10, Log1p, Logb
Rounding Floor, Ceil, Round, RoundToEven, Trunc
Trigonometry Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh
Special values Inf(sign), NaN(), IsInf(f, sign), IsNaN(f), Signbit
Bits Float64bits, Float64frombits, Float32bits, Nextafter

Example signatures:

Go
func Abs(x float64) float64
func Mod(x, y float64) float64
func IsInf(f float64, sign int) bool
func Inf(sign int) float64

IsInf with a positive sign tests for +Inf, with a negative sign for -Inf, and with 0 for either.

Edge cases: Mod takes the sign of x. Functions return NaN for inputs outside their domain (Sqrt(-1), Log(-1)) and -Inf for Log(0). NaN is never equal to anything, including itself.

Usage notes

  • Untyped constants are exact. math.MaxInt64, math.MaxUint64 and math.Pi are untyped constants with arbitrary precision, so uint64(math.MaxUint64) compiles, but fmt.Println(math.MaxUint64) alone does not (it overflows int).
  • math.Max and math.Min work on floats. For integers, use the built-in max and min (Go 1.21); see min and max. Note that math.Max(x, NaN) is NaN.
  • Never compare floats with == after arithmetic. Use a tolerance, as the example does, or work in integers (cents instead of dollars).
  • Float to integer conversion truncates toward zero and is undefined for NaN or out-of-range values; round first with math.Round().
  • Trigonometry uses radians. Multiply degrees by math.Pi/180.
  • Integer powers: math.Pow returns a float; for exact integer powers write a loop or use math/big.
  • Related packages: math/rand/v2 for random numbers, math/bits for bit manipulation, math/cmplx for complex numbers.