math

Guided tour · Math · pkg.go.dev →

Basic math: constants, elementary functions, float classification. Operates on float64.

Constants

Pi, E, and limit constants

fmt.Println(math.Pi, math.E)
fmt.Println(math.MaxFloat64, math.SmallestNonzeroFloat64)
fmt.Println(math.MaxInt, math.MinInt)          // platform int
fmt.Println(math.MaxInt64, math.MinInt64)

Elementary functions

Abs, Floor, Ceil, Round, Trunc

fmt.Println(math.Abs(-3.2))    // 3.2
fmt.Println(math.Floor(3.9))   // 3
fmt.Println(math.Ceil(3.1))    // 4
fmt.Println(math.Round(2.5))   // 3 — away from zero
fmt.Println(math.Trunc(-2.9))  // -2

Pow, Sqrt, Cbrt, Exp, Log

math.Pow(2, 10)   // 1024
math.Sqrt(16)     // 4
math.Log(math.E)  // 1
math.Log10(1000)  // 3

Trig — Sin, Cos, Tan, Atan2

Atan2(y, x) gives the angle of a point from the origin — respects quadrant.

math.Sin(math.Pi/2)   // 1
math.Atan2(1, 1)      // π/4

Min / Max / Mod

Note: Go's built-in min/max (since 1.21) work on ordered types. math.Min/Max exist but have float-specific NaN semantics.

math.Max(1.5, 2.0)   // 2
math.Mod(7, 3)       // 1 — float remainder

Float classification

IsNaN, IsInf, Inf, NaN

NaN is never equal to anything, including itself. Use IsNaN.

x := math.NaN()
fmt.Println(x == x)        // false
fmt.Println(math.IsNaN(x)) // true

y := math.Inf(+1)
fmt.Println(math.IsInf(y, +1))  // true