math Package
Go's math package provides mathematical functions including absolute value, square root, trigonometric functions, and logarithms. Commonly used functions include math.Abs(), math.Sqrt(), math.Floor(), and math.Ceil().
Syntax
import "math" // Returns the absolute value. x := math.Abs(-3.14) // 3.14 // Returns the square root. s := math.Sqrt(9.0) // 3.0 // Rounds down, rounds up, or rounds to the nearest integer. f := math.Floor(3.7) // 3.0 c := math.Ceil(3.2) // 4.0 r := math.Round(3.5) // 4.0 // Returns the larger or smaller of two values. big := math.Max(5.0, 3.0) // 5.0 small := math.Min(5.0, 3.0) // 3.0 // Uses mathematical constants. pi := math.Pi // 3.141592653589793 e := math.E // 2.718281828459045
Function List
| Function | Description |
|---|---|
| math.Abs(x) | Returns the absolute value of x. |
| math.Sqrt(x) | Returns the square root of x. |
| math.Pow(x, y) | Returns x raised to the power of y. |
| math.Floor(x) | Returns the largest integer less than or equal to x (floor). |
| math.Ceil(x) | Returns the smallest integer greater than or equal to x (ceiling). |
| math.Round(x) | Returns x rounded to the nearest integer. |
| math.Max(x, y) | Returns the larger of x and y. |
| math.Min(x, y) | Returns the smaller of x and y. |
| math.Log(x) | Returns the natural logarithm of x. |
| math.Log2(x) | Returns the base-2 logarithm of x. |
| math.Log10(x) | Returns the base-10 (common) logarithm of x. |
| math.Exp(x) | Returns e raised to the power of x. |
| math.Sin(x) / Cos(x) / Tan(x) | Returns trigonometric functions (argument in radians). |
| math.Inf(sign) | Returns positive (sign=1) or negative (sign=-1) infinity. |
| math.IsNaN(x) | Reports whether x is NaN (Not a Number). |
| math.Pi / math.E | Constants for pi and Euler's number. |
Sample Code
package main
import (
"fmt"
"math"
)
func main() {
// Examples of basic math functions.
fmt.Println("--- Abs / Sqrt / Pow ---")
fmt.Println("Abs(-7.5):", math.Abs(-7.5))
fmt.Println("Sqrt(16):", math.Sqrt(16))
fmt.Println("Pow(2, 10):", math.Pow(2, 10)) // 2 to the power of 10
fmt.Println()
// Comparing rounding methods.
fmt.Println("--- Rounding ---")
x := 3.567
fmt.Printf("Original: %.3f\n", x)
fmt.Printf("Floor: %.1f\n", math.Floor(x)) // round down
fmt.Printf("Ceil: %.1f\n", math.Ceil(x)) // round up
fmt.Printf("Round: %.1f\n", math.Round(x)) // round to nearest
fmt.Println()
// Comparing max and min values.
fmt.Println("--- Max / Min ---")
fmt.Printf("Max(12.5, 7.3): %.1f\n", math.Max(12.5, 7.3))
fmt.Printf("Min(12.5, 7.3): %.1f\n", math.Min(12.5, 7.3))
fmt.Println()
// Logarithmic and exponential functions.
fmt.Println("--- Log / Exp ---")
fmt.Printf("Log(math.E): %.4f\n", math.Log(math.E)) // natural log
fmt.Printf("Log2(8): %.1f\n", math.Log2(8)) // base-2 log
fmt.Printf("Log10(1000): %.1f\n", math.Log10(1000)) // common log
fmt.Printf("Exp(1): %.6f\n", math.Exp(1)) // e^1
fmt.Println()
// Trigonometric functions (in radians).
fmt.Println("--- Trigonometry ---")
fmt.Printf("Sin(Pi/2): %.4f\n", math.Sin(math.Pi/2)) // 1.0
fmt.Printf("Cos(Pi): %.4f\n", math.Cos(math.Pi)) // -1.0
fmt.Println()
// Checking constants.
fmt.Println("--- Constants ---")
fmt.Printf("Pi: %.15f\n", math.Pi)
fmt.Printf("E: %.15f\n", math.E)
fmt.Println("MaxFloat64:", math.MaxFloat64)
fmt.Println("MaxInt:", math.MaxInt)
}
Notes
All functions in the math package accept and return float64 values. Integer arguments are converted automatically, but if you need the integer part of a result, cast it explicitly with int(math.Floor(x)).
Taking the square root of a negative number (math.Sqrt(-1)) returns NaN. Use math.IsNaN() to check for NaN values.
For random number generation, see the math/rand package.
If you find any errors or copyright issues, please contact us.