Languages: Python C C++ Go Java Ruby Rust Perl R ← Full TOC

← Go Index

📐 math — Mathematical constants and functions

Provides basic mathematical constants and functions for float64. For integer math use operators directly. For big numbers use math/big. For random numbers use math/rand.

Import: import "math" | Docs: pkg.go.dev/math

📋 Functions, Types & Constants

NameSignature / ValueDescription
math.Absfunc Abs(x float64) float64Absolute value of x
math.Sqrtfunc Sqrt(x float64) float64Square root of x
math.Powfunc Pow(x, y float64) float64x raised to the power y
math.Pow10func Pow10(n int) float6410 raised to the power n
math.Cbrtfunc Cbrt(x float64) float64Cube root of x
math.Hypotfunc Hypot(p, q float64) float64Hypotenuse sqrt(p²+q²)
math.Ceilfunc Ceil(x float64) float64Round up to nearest integer
math.Floorfunc Floor(x float64) float64Round down to nearest integer
math.Roundfunc Round(x float64) float64Round to nearest integer
math.Truncfunc Trunc(x float64) float64Truncate toward zero
math.Modfunc Mod(x, y float64) float64Floating-point remainder of x/y
math.Maxfunc Max(x, y float64) float64Return larger of x or y
math.Minfunc Min(x, y float64) float64Return smaller of x or y
math.Logfunc Log(x float64) float64Natural logarithm of x
math.Log2func Log2(x float64) float64Base-2 logarithm of x
math.Log10func Log10(x float64) float64Base-10 logarithm of x
math.Expfunc Exp(x float64) float64e raised to the power x
math.Sinfunc Sin(x float64) float64Sine of x (radians)
math.Cosfunc Cos(x float64) float64Cosine of x (radians)
math.Tanfunc Tan(x float64) float64Tangent of x (radians)
math.Atan2func Atan2(y, x float64) float64Arc-tangent of y/x
math.Piconst Pi = 3.14159265358979323846The mathematical constant π
math.Econst E = 2.71828182845904523536Euler's number e
math.Phiconst Phi = 1.61803398874989484820Golden ratio φ
math.Sqrt2const Sqrt2 = 1.41421356237309504880Square root of 2
math.MaxFloat64const = 1.7976931348623157e+308Largest finite float64
math.SmallestNonzeroFloat64constSmallest positive nonzero float64
math.MaxIntconstMaximum int value (platform dependent)
math.IsNaNfunc IsNaN(f float64) boolReport if f is NaN
math.IsInffunc IsInf(f float64, sign int) boolReport if f is infinity
math.NaNfunc NaN() float64Return IEEE 754 'not-a-number' value

💡 Example Program

Save as main.go inside any directory and run with go run main.go. No extra packages to install — all standard library.

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Printf("Pi        = %.10f\n", math.Pi)
    fmt.Printf("E         = %.10f\n", math.E)
    fmt.Printf("Sqrt(2)   = %.10f\n", math.Sqrt(2))
    fmt.Printf("Pow(2,10) = %.0f\n",  math.Pow(2, 10))
    fmt.Printf("Log(e)    = %.4f\n",  math.Log(math.E))
    fmt.Printf("Sin(π/6)  = %.4f\n",  math.Sin(math.Pi/6))
    fmt.Printf("Cos(π/3)  = %.4f\n",  math.Cos(math.Pi/3))
    fmt.Printf("Hypot(3,4)= %.1f\n",  math.Hypot(3, 4))
    fmt.Printf("Floor(3.9)= %.1f\n",  math.Floor(3.9))
    fmt.Printf("Ceil(3.1) = %.1f\n",  math.Ceil(3.1))
    fmt.Printf("Round(3.5)= %.1f\n",  math.Round(3.5))
    fmt.Printf("Abs(-7.5) = %.1f\n",  math.Abs(-7.5))
    fmt.Printf("Max(3,7)  = %.1f\n",  math.Max(3, 7))
    fmt.Printf("MaxFloat64= %e\n",    math.MaxFloat64)

    // Quadratic formula roots for x² - 5x + 6 = 0
    a, b, c := 1.0, -5.0, 6.0
    disc := b*b - 4*a*c
    x1 := (-b + math.Sqrt(disc)) / (2 * a)
    x2 := (-b - math.Sqrt(disc)) / (2 * a)
    fmt.Printf("Roots: x1=%.1f  x2=%.1f\n", x1, x2)
}
// go run main.go

← strconv  |  🏠 Index  |  time →