5 minutes read

Now that you have some knowledge about basic arithmetic operations in Go, it's time to learn how to perform advanced numeric operations, such as finding the maximum or minimum of two numbers, rounding a number up or down, and even more complex operations like working with trigonometric functions.

In Go, the math package from the standard library provides useful mathematical functions and constants; in this topic, we'll learn about the most useful functions of the math package.

Advanced arithmetic functions

There are many functions within the math package; we'll start off by taking a look at the functions that can help us perform the most common numeric operations on float64 types, such as:

  • math.Abs(x) — returns the absolute value of its argument;

  • math.Min(x, y) — returns the minimum value between two arguments;

  • math.Max(x, y) — returns the maximum value between two arguments.

Now, let's take a look at how we can use the above functions in a Go program:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println(math.Abs(-1.618)) // Abs is 1.618

    fmt.Println(math.Min(0, -6)) // Min is -6
    fmt.Println(math.Max(3, 2))  // Max is 3
}

Take notice that the above functions take float64 types as arguments and return float64 types... so, if you want to calculate the maximum value between two predefined int type variables, you'll need to cast the arguments as float64 types using the following syntax:

...

func main() {
    var x, y int = 3, 4

    // We cast 'x' and 'y' as float64 types below:
    fmt.Println(math.Max(float64(x), float64(y))) // Max is 4
}

Rounding functions

Another basic numeric operation is rounding a number up or down. The math package provides three convenient functions to round float64 types:

  • math.Floor(x) — returns the greatest integer value less than or equal to its argument;

  • math.Ceil(x) — returns the least integer value greater than or equal to its argument;

  • math.Round(x) — returns the nearest integer value, rounding half away from zero.

Below are simple use cases of the rounding functions within a Go program:

...

func main() {
    fmt.Println(math.Floor(3.43)) // Floor is 3
    fmt.Println(math.Ceil(1.71))  // Ceil is 2

    roundUp := math.Round(7.50)
    fmt.Println(roundUp) // roundUp is 8

    roundDown := math.Round(7.49)
    fmt.Println(roundDown) // roundDown is 7
}

Let's take a detailed look at the math.Round() function. It will round up a number if its decimals are greater than or equal to >= 0.5; in contrast, if the number decimals are less than < 0.5, then it will round the number down.

Constants in the math package

So far, we've seen functions that allow us to perform basic numeric operations; however, the math package also contains important numeric constants, such as Pi (π) and Euler's number (e):

  • math.Pi — a constant for Pi; it is the ratio of the circumference of a circle to its diameter;

  • math.E — a constant for Euler's number; it is the base for natural logarithms;

  • math.MinInt — a constant for the minimum integer limit value;

  • math.MaxInt — a constant for the maximum integer limit value.

Let's take a look at the value of each one of these constants in a Go program:

...

func main() {
    fmt.Println(math.Pi) // Pi is 3.141592653589793...
    fmt.Println(math.E)  // E is 2.718281828459045...

    fmt.Println(math.MinInt) // MinInt is -9223372036854775808
    fmt.Println(math.MaxInt) // MaxInt is 9223372036854775807
}

The math package contains many other numeric constants; you can take a look at them in the official math package documentation.

Exponential and logarithmic functions

When we need to perform more advanced mathematical operations on float64 types, such as calculating the square/cube root of a number, raising a number to a certain power, or calculating the natural logarithm of a number, we can use the following functions:

  • math.Sqrt(x) — returns the square root √ of its argument;

  • math.Cbrt(x) — returns the cube root ∛ of its argument;

  • math.Pow(x, y) — returns the value of the first argument raised to the power of the second argument;

  • math.Log(x) — returns the natural logarithm of its argument.

...

func main() {
    fmt.Println(math.Sqrt(4))   // Sqrt is 2
    fmt.Println(math.Pow(2, 2)) // the square of 2 is 4

    fmt.Println(math.Cbrt(64))  // Cbrt is 4
    fmt.Println(math.Pow(4, 3)) // the cube of 4 is 64

    fmt.Println(math.Log(1))      // Log of 1 is 0
    fmt.Println(math.Log(math.E)) // Log of math.E is 1
}

Take a look at what happens when we calculate the natural logarithm of the math.E constant. Since math.E is the base for natural logarithms, its natural logarithm value is exactly 1.

Trigonometric functions

There are many trigonometric functions in the math package. These trigonometric functions take radians of the float64 type as arguments.

The most commonly used trigonometric functions are the following:

  • math.Cos(x) — returns the cosine of its radian argument;

  • math.Sin(x) — returns the sine of its radian argument;

  • math.Tan(x) — returns the tangent of its radian argument.

As mentioned previously, the above functions take radians as arguments; so, if we wanted to calculate the cosine of 60° (degrees), we would need to apply the degrees to radians formula — degrees * math.Pi / 180. Let's take a look at how we can do this in Go:

...

func main() {
    degrees := 60.0

    // Below we apply the degrees to radians formula:
    radians := degrees * math.Pi / 180 // 60.0 degrees are 1.04719... radians

    fmt.Println(math.Cos(radians)) // Cos is 0.5000...
    fmt.Println(math.Sin(radians)) // Sin is 0.8660...
    fmt.Println(math.Tan(radians)) // Tan is 1.7320...
}

There are many more trigonometric functions within the math package, such as the inverse trigonometric functions like math.Acos() used to calculate the arccosine of its argument, or the hyperbolic functions like math.Cosh() used to calculate the hyperbolic cosine of its argument. If you want to learn more about these trigonometric functions, take a look at the official math package documentation.

Summary

In this topic, we've learned how to perform basic numeric operations such as rounding a number up or down, as well as more complex operations like converting from degrees to radians and calculating the cosine of an angle value in radians. The math package in Go provides many helpful functions that will make your life easier when you need to perform both simple and complex mathematical operations.

Now it's time to test your knowledge about the math package and solve some theory and coding tasks. The coding tasks will be the most interesting, as you will see how we can use the math package to calculate volumes and other trigonometric identities.

25 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo