Go's math package provides basic constants and mathematical functions operating on float64: rounding, powers, roots, trigonometry, and more.
Examples
Round breaks ties away from zero. Ceil and Floor never round toward zero, even for negative inputs.
fmt.Println(math.Abs(-7.3))
fmt.Println(math.Ceil(4.1))
fmt.Println(math.Floor(4.9))
fmt.Println(math.Round(2.5))
fmt.Println(math.Trunc(4.9))7.3
5
4
3
4Pow and Sqrt cover the common cases; Log without a subscript is the natural log, base e.
fmt.Println(math.Sqrt(64))
fmt.Println(math.Cbrt(27))
fmt.Println(math.Pow(2, 10))
fmt.Println(math.Exp(1))
fmt.Println(math.Log(math.E))8
3
1024
2.718281828459045
1Every trig function takes and returns radians, not degrees. Atan2 takes y, x in that order so it can pick the correct quadrant.
fmt.Println(math.Sin(math.Pi / 2))
fmt.Println(math.Cos(0))
fmt.Println(math.Atan2(1, 1))
fmt.Println(math.Hypot(3, 4))1
1
0.7853981633974483
5IsNaN is the only reliable way to check for NaN — a NaN value never equals itself, so x == math.NaN() is always false.
fmt.Println(math.Max(3, 7))
fmt.Println(math.Min(3, 7))
fmt.Println(math.IsNaN(math.NaN()))
fmt.Println(math.IsInf(math.Inf(1), 1))7
3
true
true| Function | Description |
|---|---|
Abs(x float64) float64 | Absolute value of x. |
Ceil(x float64) float64 | Smallest integer value >= x. |
Floor(x float64) float64 | Largest integer value <= x. |
Round(x float64) float64 | Rounds to the nearest integer, halves away from zero. |
| |
RoundToEven(x float64) float64 | Rounds to the nearest integer, halves to the even neighbor. |
Trunc(x float64) float64 | Integer value of x toward zero (drops the fraction). |
Copysign(f, sign float64) float64 | Magnitude of f with the sign of sign. |
Dim(x, y float64) float64 | Maximum of x-y or 0. |
Mod(x, y float64) float64 | Floating-point remainder of x/y. |
Modf(f float64) (int float64, frac float64) | Splits f into integer and fractional parts. |
| |
Sqrt(x float64) float64 math.Sqrt(64) // 8 | Square root of x. |
Cbrt(x float64) float64 | Cube root of x. |
Pow(x, y float64) float64 math.Pow(2, 10) // 1024 | x raised to the power y. |
Pow10(n int) float64 | 10 raised to the power n. |
Exp(x float64) float64 | e raised to the power x. |
Exp2(x float64) float64 | 2 raised to the power x. |
Expm1(x float64) float64 | e**x - 1, accurate for x near 0. |
Log(x float64) float64 | Natural logarithm of x. |
Log10(x float64) float64 | Base-10 logarithm of x. |
Log2(x float64) float64 | Base-2 logarithm of x. |
Log1p(x float64) float64 | Natural log of 1+x, accurate for x near 0. |
Logb(x float64) float64 | Binary exponent of x, as a float64. |
Ilogb(x float64) int | Binary exponent of x, as an int. |
Hypot(p, q float64) float64 math.Hypot(3, 4) // 5 | sqrt(p*p + q*q), computed to avoid overflow/underflow. |
FMA(x, y, z float64) float64 | x*y + z, rounded once (fused multiply-add). |
Sin(x float64) float64 | Sine of x radians. |
Cos(x float64) float64 | Cosine of x radians. |
Tan(x float64) float64 | Tangent of x radians. |
Asin(x float64) float64 | Arcsine of x, in radians. |
Acos(x float64) float64 | Arccosine of x, in radians. |
Atan(x float64) float64 | Arctangent of x, in radians. |
Atan2(y, x float64) float64 | Arctangent of y/x, using the sign of both to pick the quadrant. |
Sincos(x float64) (sin float64, cos float64) | Sine and cosine of x in one call. |
Sinh(x float64) float64 | Hyperbolic sine of x. |
Cosh(x float64) float64 | Hyperbolic cosine of x. |
Tanh(x float64) float64 | Hyperbolic tangent of x. |
Asinh(x float64) float64 | Inverse hyperbolic sine of x. |
Acosh(x float64) float64 | Inverse hyperbolic cosine of x. |
Atanh(x float64) float64 | Inverse hyperbolic tangent of x. |
Erf(x float64) float64 | Error function of x. |
Erfc(x float64) float64 | Complementary error function of x. |
Erfinv(x float64) float64 | Inverse error function of x. |
Erfcinv(x float64) float64 | Inverse complementary error function of x. |
Gamma(x float64) float64 | Gamma function of x. |
Lgamma(x float64) (lgamma float64, sign int) | Natural log of |Gamma(x)|, and its sign. |
J0(x float64) float64 | Bessel function of the first kind, order 0. |
J1(x float64) float64 | Bessel function of the first kind, order 1. |
Jn(n int, x float64) float64 | Bessel function of the first kind, order n. |
Y0(x float64) float64 | Bessel function of the second kind, order 0. |
Y1(x float64) float64 | Bessel function of the second kind, order 1. |
Yn(n int, x float64) float64 | Bessel function of the second kind, order n. |
Float32bits(f float32) uint32 | IEEE 754 binary representation of f. |
Float32frombits(b uint32) float32 | float32 from its IEEE 754 binary representation. |
Float64bits(f float64) uint64 | IEEE 754 binary representation of f. |
Float64frombits(b uint64) float64 | float64 from its IEEE 754 binary representation. |
Frexp(f float64) (frac float64, exp int) | Breaks f into a normalized fraction and a power of two. |
| |
Ldexp(frac float64, exp int) float64 | frac * 2**exp; the inverse of Frexp. |
Nextafter(x, y float64) float64 | Next float64 after x, in the direction of y. |
Nextafter32(x, y float32) float32 | Next float32 after x, in the direction of y. |
Max(x, y float64) float64 | Larger of x or y. |
Min(x, y float64) float64 | Smaller of x or y. |
Remainder(x, y float64) float64 | IEEE 754 floating-point remainder of x/y. |
IsNaN(f float64) bool | Reports whether f is an IEEE 754 NaN. |
IsInf(f float64, sign int) bool | Reports whether f is infinite, matching sign if sign != 0. |
Signbit(x float64) bool | Reports whether x is negative or negative zero. |
Inf(sign int) float64 | Positive infinity if sign >= 0, else negative infinity. |
NaN() float64 | An IEEE 754 not-a-number value. |
Pi | 3.14159265358979323846... |
E | 2.71828182845904523536... |
Phi | 1.61803398874989484820... (golden ratio) |
Sqrt2 | 1.41421356237309504880... |
MaxFloat64 | Largest representable float64. |
SmallestNonzeroFloat64 | Smallest positive nonzero float64. |