Search⌘ K
AI Features

The math Package

Explore how to use Go’s math package to perform common mathematical operations and work with built-in constants. Understand functions like Abs, Sqrt, Pow, and Round, and learn how Go handles floating-point values such as NaN and Infinity. This lesson equips you with essential skills to perform accurate and efficient numerical computations in Go.

We'll cover the following...

Overview

The math package provides basic mathematical constants and functions for floating-point arithmetic (specifically float64). It defines commonly used constants:

  • math.Pi → π (3.141592653589793)

  • math.E → Euler’s number

  • math.Sqrt2

  • math.Ln2, math.Ln10

  • math.MaxFloat64, math.SmallestNonzeroFloat64

For information, visit this page.

Explanation

Let's see how to use a few of its most frequently used functions.

  • math.Abs(x) returns the absolute value of a number, which means it removes the negative sign if one exists.

  • math.Sqrt(x) calculates the square root of a non-negative number.

  • math.Pow(x, y) raises a number x to the power of y.

  • math.Round(x) rounds a floating-point number to the nearest whole number.

The following example demonstrates how these functions are used in practice:

Go (1.24.2)
package main
import (
"fmt"
"math"
)
func main() {
x := -9.7
absValue := math.Abs(x) // Absolute value
sqrtValue := math.Sqrt(16) // Square root
powerValue := math.Pow(2, 3) // Power calculation
roundedValue := math.Round(3.6) // Rounding
fmt.Println("Absolute value:", absValue)
fmt.Println("Square root:", sqrtValue)
fmt.Println("Power:", powerValue)
fmt.Println("Rounded value:", roundedValue)
}

In the code above, the math package is imported to access mathematical utilities at line 5. Inside the main function, a floating-point value x is defined with a negative number. At line 11, the math.Abs() function converts this value to its positive form. At line 12, math.Sqrt() calculates the square root of 16. At line 13, math.Pow() raises 2 to the power of 3, and at line 14 math.Round() rounds 3.6 to the nearest whole number. Finally, the results of each calculation are printed to the console, showing how the math package simplifies common mathematical operations in Go.

Additional details

Remember, that all functions in the math package operate on values of type float64. If you are working with integers, you must explicitly convert them to float64 before using any math functions. This design keeps the package consistent and avoids implicit type conversions.

The package also includes support for special floating-point values such as NaN (Not a Number) and Infinity. Functions like math.NaN(), math.IsNaN(), and math.Inf() are useful when dealing with invalid results, divisions by zero, or extreme values in scientific and financial calculations.

In addition to math, Go offers related packages for specialized needs. The math/rand package is used for random number generation, math/big supports arbitrary-precision arithmetic, and math/cmplx handles complex number operations. Together, these packages cover most numerical use cases in Go.