Math Functions

Published Jun 9, 2023
Contribute to Docs

Go supports many different math functions through the math package. The math package must be imported before using these functions, as in the example below:

package main
import (
"fmt"
"math"
)
func main() {
number := math.Round(-2.55)
fmt.Printf("%.1f\n", number)
}

Below is a list of math functions usable through the math package.

Math Functions

Abs()
Returns the absolute value of a given number.
Acos()
Returns the inverse of the cosine value of a number.
Acosh()
Returns the inverse hyperbolic cosine of a number.
Asin()
Returns the inverse sine of a number.
Asinh()
Returns the inverse hyperbolic sine of a number.
Atan()
Returns the arctangent of the given value.
Atan2()
Returns the arctangent value of the x/y value.
Atanh()
Returns the inverse hyperbolic tangent of the given value.
Cbrt()
Returns the cube root of a given number of type float64.
Ceil()
Returns a given decimal number rounded up to the next highest integer.
Copysign()
Returns a value with the magnitude and sign of the given arguments.
Cos()
Returns the cosine of the given angle.
Cosh()
Returns the hyperbolic cosine of a given value.
Dim()
Returns the maximum of the difference between two arguments.
Exp()
Returns the value of e raised to the power of the parameter x.
Exp2()
Returns the value of a base-2 exponential of a given number.
Expm1()
Returns the value of e raised to the power of the given parameter minus 1.
Floor()
Returns the given decimal number rounded down to the nearest whole number.
Hypot()
Returns the square root of the sum of two squares.
Log()
Returns the natural logarithm of a given number.
Log10()
Returns the base-10 logarithm of a given number.
Log2()
Returns the base-2 logarithm of a given number.
Max()
Returns the maximum value of two specified numbers.
Min()
Returns the minimum value of two specified numbers.
Mod()
Returns the floating-point remainder of dividing x by y.
Modf()
Returns the integer and fractional part of a floating-point number.
Pow()
Returns the first argument raised to the power of the second argument.
Remainder()
Returns the remainder of the division of two given values.
Round()
Rounds the given number to the nearest integer.
RoundToEven()
Rounds a floating-point number to the nearest even integer.
Sin()
Used to calculate the sine of an angle.
Sinh()
Returns the hyperbolic sine of the given number.
Sqrt()
Returns the square root of a given number.
Tan()
Returns the tangent of the given angle.
Tanh()
Returns the hyperbolic tangent of a number.
Trunc()
Returns the integer value of the given number.

All contributors

Looking to contribute?

Learn Go on Codecademy