codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

36 / 72 topics
27Standard Library Overview28IO Package29Net Package30HTTP Package31JSON Package32XML Package33Time Package34OS Package35Fmt Package36Math Package37Regexp Package38Log Package39Flag Package40Context Package41Embed Package
Tutorials/Go (Golang)/Math Package
🐹Go (Golang)

Math Package

Updated 2026-04-20
3 min read

Math Package

The math package in Go is a fundamental part of the standard library, providing a wide range of mathematical functions and constants that are essential for various computations. This tutorial will delve into the details of the math package, covering its key features, real-world applications, and best practices.

Overview

The math package offers a variety of functionalities including:

  • Basic arithmetic operations
  • Trigonometric functions
  • Exponential and logarithmic functions
  • Special functions like gamma and error functions
  • Constants such as π (pi) and e (Euler's number)

To use the math package, you need to import it at the beginning of your Go file:

import (
    "fmt"
    "math"
)

Basic Arithmetic Operations

The math package provides a set of functions for basic arithmetic operations that can be more precise than using native operators.

Absolute Value

The Abs function returns the absolute value of a float64 number:

func main() {
    fmt.Println(math.Abs(-10)) // Output: 10
}

Ceiling and Floor

  • Ceil: Returns the smallest integer greater than or equal to x.
  • Floor: Returns the largest integer less than or equal to x.
func main() {
    fmt.Println(math.Ceil(4.3))  // Output: 5
    fmt.Println(math.Floor(4.7)) // Output: 4
}

Modulus

The Mod function returns the floating-point remainder of x/y:

func main() {
    fmt.Println(math.Mod(10, 3)) // Output: 1
}

Trigonometric Functions

The math package includes a comprehensive set of trigonometric functions.

Sine and Cosine

func main() {
    fmt.Println(math.Sin(math.Pi / 2)) // Output: 1
    fmt.Println(math.Cos(0))           // Output: 1
}

Tangent

func main() {
    fmt.Println(math.Tan(math.Pi / 4)) // Output: 0.9999999999999999
}

Exponential and Logarithmic Functions

The package provides functions for exponential and logarithmic calculations.

Exponentiation

  • Exp: Returns e raised to the power of x.
  • Pow: Returns x raised to the power of y.
func main() {
    fmt.Println(math.Exp(1)) // Output: 2.718281828459045
    fmt.Println(math.Pow(2, 3)) // Output: 8
}

Logarithms

  • Log: Returns the natural logarithm of x.
  • Log10: Returns the base-10 logarithm of x.
func main() {
    fmt.Println(math.Log(math.E))   // Output: 1
    fmt.Println(math.Log10(100))     // Output: 2
}

Special Functions

The math package also includes some special mathematical functions:

Gamma Function

The gamma function is an extension of the factorial function to real and complex numbers.

func main() {
    fmt.Println(math.Gamma(5)) // Output: 24 (which is 4!)
}

Error Function

The error function is a special function used in probability, statistics, and partial differential equations.

func main() {
    fmt.Println(math.Erf(1)) // Output: 0.8427007929497148
}

Constants

The math package defines several mathematical constants:

  • Pi (π): The ratio of a circle's circumference to its diameter.
  • Euler's Number (e): Base of the natural logarithm.
func main() {
    fmt.Println(math.Pi)  // Output: 3.141592653589793
    fmt.Println(math.E)   // Output: 2.718281828459045
}

Best Practices

  1. Precision: Always be aware of the precision limitations of floating-point arithmetic.
  2. Error Handling: For functions that can return errors (like math.Sqrt for negative inputs), handle potential errors gracefully.
  3. Constants: Use constants from the math package instead of hardcoding values to maintain consistency and readability.

Real-World Applications

The math package is widely used in various applications such as:

  • Physics Simulations: Calculating trajectories, forces, and other physical phenomena.
  • Financial Modeling: Computing interest rates, risk assessments, and portfolio optimizations.
  • Graphics Rendering: Handling transformations, projections, and color calculations.

Conclusion

The math package in Go is a powerful tool for performing mathematical operations. By understanding its functions and constants, you can write more efficient and accurate code. Always refer to the official documentation for the most up-to-date information and additional features.


This tutorial provides a comprehensive guide to the math package in Go, covering its key functionalities, best practices, and real-world applications.


PreviousFmt PackageNext Regexp Package

Recommended Gear

Fmt PackageRegexp Package