math

package standard library
go1.26.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2026 License: BSD-3-Clause Imports: 3 Imported by: 615,550

Documentation

Overview

Package math provides basic constants and mathematical functions.

This package does not guarantee bit-identical results across architectures.

Index

Examples

Constants

View Source
const (
	E   = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113
	Pi  = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
	Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622

	Sqrt2   = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193
	SqrtE   = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774
	SqrtPi  = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161
	SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339

	Ln2    = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162
	Log2E  = 1 / Ln2
	Ln10   = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392
	Log10E = 1 / Ln10
)

Mathematical constants.

View Source
const (
	MaxFloat32             = 0x1p127 * (1 + (1 - 0x1p-23)) // 3.40282346638528859811704183484516925440e+38
	SmallestNonzeroFloat32 = 0x1p-126 * 0x1p-23            // 1.401298464324817070923729583289916131280e-45

	MaxFloat64             = 0x1p1023 * (1 + (1 - 0x1p-52)) // 1.79769313486231570814527423731704356798070e+308
	SmallestNonzeroFloat64 = 0x1p-1022 * 0x1p-52            // 4.9406564584124654417656879286822137236505980e-324
)

Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

View Source
const (
	MaxInt    = 1<<(intSize-1) - 1  // MaxInt32 or MaxInt64 depending on intSize.
	MinInt    = -1 << (intSize - 1) // MinInt32 or MinInt64 depending on intSize.
	MaxInt8   = 1<<7 - 1            // 127
	MinInt8   = -1 << 7             // -128
	MaxInt16  = 1<<15 - 1           // 32767
	MinInt16  = -1 << 15            // -32768
	MaxInt32  = 1<<31 - 1           // 2147483647
	MinInt32  = -1 << 31            // -2147483648
	MaxInt64  = 1<<63 - 1           // 9223372036854775807
	MinInt64  = -1 << 63            // -9223372036854775808
	MaxUint   = 1<<intSize - 1      // MaxUint32 or MaxUint64 depending on intSize.
	MaxUint8  = 1<<8 - 1            // 255
	MaxUint16 = 1<<16 - 1           // 65535
	MaxUint32 = 1<<32 - 1           // 4294967295
	MaxUint64 = 1<<64 - 1           // 18446744073709551615
)

Integer limit values.

Variables

This section is empty.

Functions

func Abs

func Abs(x float64) float64

Abs returns the absolute value of x.

Special cases are:

Abs(±Inf) = +Inf
Abs(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	x := math.Abs(-2)
	fmt.Printf("%.1f\n", x)

	y := math.Abs(2)
	fmt.Printf("%.1f\n", y)
}
Output:
2.0
2.0

func Acos

func Acos(x float64) float64

Acos returns the arccosine, in radians, of x.

Special case is:

Acos(x) = NaN if x < -1 or x > 1
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Acos(1))
}
Output:
0.00

func Acosh

func Acosh(x float64) float64

Acosh returns the inverse hyperbolic cosine of x.

Special cases are:

Acosh(+Inf) = +Inf
Acosh(x) = NaN if x < 1
Acosh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Acosh(1))
}
Output:
0.00

func Asin

func Asin(x float64) float64

Asin returns the arcsine, in radians, of x.

Special cases are:

Asin(±0) = ±0
Asin(x) = NaN if x < -1 or x > 1
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Asin(0))
}
Output:
0.00

func Asinh

func Asinh(x float64) float64

Asinh returns the inverse hyperbolic sine of x.

Special cases are:

Asinh(±0) = ±0
Asinh(±Inf) = ±Inf
Asinh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Asinh(0))
}
Output:
0.00

func Atan

func Atan(x float64) float64

Atan returns the arctangent, in radians, of x.

Special cases are:

Atan(±0) = ±0
Atan(±Inf) = ±Pi/2
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Atan(0))
}
Output:
0.00

func Atan2

func Atan2(y, x float64) float64

Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.

Special cases are (in order):

Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(+0, x>=0) = +0
Atan2(-0, x>=0) = -0
Atan2(+0, x<=-0) = +Pi
Atan2(-0, x<=-0) = -Pi
Atan2(y>0, 0) = +Pi/2
Atan2(y<0, 0) = -Pi/2
Atan2(+Inf, +Inf) = +Pi/4
Atan2(-Inf, +Inf) = -Pi/4
Atan2(+Inf, -Inf) = 3Pi/4
Atan2(-Inf, -Inf) = -3Pi/4
Atan2(y, +Inf) = 0
Atan2(y>0, -Inf) = +Pi
Atan2(y<0, -Inf) = -Pi
Atan2(+Inf, x) = +Pi/2
Atan2(-Inf, x) = -Pi/2
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Atan2(0, 0))
}
Output:
0.00

func Atanh

func Atanh(x float64) float64

Atanh returns the inverse hyperbolic tangent of x.

Special cases are:

Atanh(1) = +Inf
Atanh(±0) = ±0
Atanh(-1) = -Inf
Atanh(x) = NaN if x < -1 or x > 1
Atanh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Atanh(0))
}
Output:
0.00

func Cbrt

func Cbrt(x float64) float64

Cbrt returns the cube root of x.

Special cases are:

Cbrt(±0) = ±0
Cbrt(±Inf) = ±Inf
Cbrt(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Cbrt(8))
	fmt.Printf("%.2f\n", math.Cbrt(27))
}
Output:
2.00
3.00

func Ceil

func Ceil(x float64) float64

Ceil returns the least integer value greater than or equal to x.

Special cases are:

Ceil(±0) = ±0
Ceil(±Inf) = ±Inf
Ceil(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	c := math.Ceil(1.49)
	fmt.Printf("%.1f", c)
}
Output:
2.0

func Copysign

func Copysign(f, sign float64) float64

Copysign returns a value with the magnitude of f and the sign of sign.

Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Copysign(3.2, -1))
}
Output:
-3.20

func Cos

func Cos(x float64) float64

Cos returns the cosine of the radian argument x.

Special cases are:

Cos(±Inf) = NaN
Cos(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Cos(math.Pi/2))
}
Output:
0.00

func Cosh

func Cosh(x float64) float64

Cosh returns the hyperbolic cosine of x.

Special cases are:

Cosh(±0) = 1
Cosh(±Inf) = +Inf
Cosh(NaN) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f", math.Cosh(0))
}
Output:
1.00

func Dim

func Dim(x, y float64) float64

Dim returns the maximum of x-y or 0.

Special cases are:

Dim(+Inf, +Inf) = NaN
Dim(-Inf, -Inf) = NaN
Dim(x, NaN) = Dim(NaN, x) = NaN
Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Dim(4, -2))
	fmt.Printf("%.2f\n", math.Dim(-4, 2))
}
Output:
6.00
0.00

func Erf

func Erf(x float64) float64

Erf returns the error function of x.

Special cases are:

Erf(+Inf) = 1
Erf(-Inf) = -1
Erf(NaN) = NaN

func Erfc

func Erfc(x float64) float64

Erfc returns the complementary error function of x.

Special cases are:

Erfc(+Inf) = 0
Erfc(-Inf) = 2
Erfc(NaN) = NaN

func Erfcinv added in go1.10

func Erfcinv(x float64) float64

Erfcinv returns the inverse of Erfc(x).

Special cases are:

Erfcinv(0) = +Inf
Erfcinv(2) = -Inf
Erfcinv(x) = NaN if x < 0 or x > 2
Erfcinv(NaN) = NaN

func Erfinv added in go1.10

func Erfinv(x float64) float64

Erfinv returns the inverse error function of x.

Special cases are:

Erfinv(1) = +Inf
Erfinv(-1) = -Inf
Erfinv(x) = NaN if x < -1 or x > 1
Erfinv(NaN) = NaN

func Exp

func Exp(x float64) float64

Exp returns e**x, the base-e exponential of x.

Special cases are:

Exp(+Inf) = +Inf
Exp(NaN) = NaN

Very large values overflow to 0 or +Inf. Very small values underflow to 1.

Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Exp(1))
	fmt.Printf("%.2f\n", math.Exp(2))
	fmt.Printf("%.2f\n", math.Exp(-1))
}
Output:
2.72
7.39
0.37

func Exp2

func Exp2(x float64) float64

Exp2 returns 2**x, the base-2 exponential of x.

Special cases are the same as Exp.

Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.2f\n", math.Exp2(1))
	fmt.Printf("%.2f\n", math.Exp2(-3))
}
Output:
2.00
0.12

func Expm1

func Expm1(x float64) float64

Expm1 returns e**x - 1, the base-e exponential of x minus 1. It is more accurate than Exp(x) - 1 when x is near zero.

Special cases are:

Expm1(+Inf) = +Inf
Expm1(-Inf) = -1
Expm1(NaN) = NaN

Very large values overflow to -1 or +Inf.

Example
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("%.6f\n", math.Expm1(0.01))
	fmt.Printf("%.6f\n", math.Expm1(-1))
}
Output:
0.010050
-0.632121

func FMA added in go1.14

func FMA(x, y, z float64) float64

FMA returns x * y + z, computed with only one rounding. (That is, FMA returns the fused multiply-add of x, y, and z.)

func