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
| Name | Signature / Value | Description |
|---|---|---|
| math.Abs | func Abs(x float64) float64 | Absolute value of x |
| math.Sqrt | func Sqrt(x float64) float64 | Square root of x |
| math.Pow | func Pow(x, y float64) float64 | x raised to the power y |
| math.Pow10 | func Pow10(n int) float64 | 10 raised to the power n |
| math.Cbrt | func Cbrt(x float64) float64 | Cube root of x |
| math.Hypot | func Hypot(p, q float64) float64 | Hypotenuse sqrt(p²+q²) |
| math.Ceil | func Ceil(x float64) float64 | Round up to nearest integer |
| math.Floor | func Floor(x float64) float64 | Round down to nearest integer |
| math.Round | func Round(x float64) float64 | Round to nearest integer |
| math.Trunc | func Trunc(x float64) float64 | Truncate toward zero |
| math.Mod | func Mod(x, y float64) float64 | Floating-point remainder of x/y |
| math.Max | func Max(x, y float64) float64 | Return larger of x or y |
| math.Min | func Min(x, y float64) float64 | Return smaller of x or y |
| math.Log | func Log(x float64) float64 | Natural logarithm of x |
| math.Log2 | func Log2(x float64) float64 | Base-2 logarithm of x |
| math.Log10 | func Log10(x float64) float64 | Base-10 logarithm of x |
| math.Exp | func Exp(x float64) float64 | e raised to the power x |
| math.Sin | func Sin(x float64) float64 | Sine of x (radians) |
| math.Cos | func Cos(x float64) float64 | Cosine of x (radians) |
| math.Tan | func Tan(x float64) float64 | Tangent of x (radians) |
| math.Atan2 | func Atan2(y, x float64) float64 | Arc-tangent of y/x |
| math.Pi | const Pi = 3.14159265358979323846 | The mathematical constant π |
| math.E | const E = 2.71828182845904523536 | Euler's number e |
| math.Phi | const Phi = 1.61803398874989484820 | Golden ratio φ |
| math.Sqrt2 | const Sqrt2 = 1.41421356237309504880 | Square root of 2 |
| math.MaxFloat64 | const = 1.7976931348623157e+308 | Largest finite float64 |
| math.SmallestNonzeroFloat64 | const | Smallest positive nonzero float64 |
| math.MaxInt | const | Maximum int value (platform dependent) |
| math.IsNaN | func IsNaN(f float64) bool | Report if f is NaN |
| math.IsInf | func IsInf(f float64, sign int) bool | Report if f is infinity |
| math.NaN | func NaN() float64 | Return IEEE 754 'not-a-number' value |
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