Skip to content

Math Module

The std/math module provides comprehensive mathematical functions and constants with full Go math package parity.

import { math } from "std/math"

Returns Pi (3.14159…).

Returns Euler’s number (2.71828…).

Returns positive infinity if sign >= 0, negative infinity if sign < 0.

Returns an IEEE 754 “not-a-number” value.

println(math.PI()) // 3.141592653589793
println(math.E()) // 2.718281828459045
println(math.inf(1)) // +Inf
println(math.nan()) // NaN

Returns the absolute value of a number.

println(math.abs(-5.0)) // 5
println(math.abs(3.7)) // 3.7

Rounds up to the nearest integer.

Rounds down to the nearest integer.

Rounds to the nearest integer.

Rounds to the nearest integer, rounding ties to even.

println(math.roundToEven(0.5)) // 0
println(math.roundToEven(1.5)) // 2
println(math.roundToEven(2.5)) // 2

Returns the integer value of x (truncates toward zero).

println(math.trunc(2.7)) // 2
println(math.trunc(-2.7)) // -2

Returns the smaller of two values.

Returns the larger of two values.

Returns max(x - y, 0).

println(math.dim(5.0, 3.0)) // 2
println(math.dim(3.0, 5.0)) // 0

math.copysign(f: float, sign: float): float

Section titled “math.copysign(f: float, sign: float): float”

Returns a value with the magnitude of f and the sign of sign.

Reports whether the sign bit of x is set (negative or negative zero).

Returns a random float between 0.0 (inclusive) and 1.0 (exclusive).

Returns the square root.

Returns the cube root.

println(math.cbrt(27.0)) // 3
println(math.cbrt(8.0)) // 2

Returns base raised to the power of exp.

Returns 10 raised to the power of n.

println(math.pow10(3)) // 1000

Returns sqrt(p*p + q*q), taking care not to overflow or underflow.

println(math.hypot(3.0, 4.0)) // 5

Returns e**x.

Returns 2**x.

Returns e**x - 1, more accurate than exp(x) - 1 when x is near zero.

Returns the natural logarithm (base e).

Returns log(1 + x), more accurate when x is near zero.

Returns the base-2 logarithm.

Returns the base-10 logarithm.

Returns the binary exponent of x as a float.

Returns the binary exponent of x as an integer.

Returns frac × 2**exp (inverse of frexp).

Returns [frac, exp] such that x == frac × 2**exp.

var result = math.frexp(4.0)
println(result[0]) // 0.5
println(result[1]) // 3

Returns the sine of x (radians).

Returns the cosine of x (radians).

Returns the tangent of x (radians).

Returns [sin(x), cos(x)].

var sc = math.sincos(0.0)
println(sc[0]) // 0 (sin)
println(sc[1]) // 1 (cos)

Returns the arccosine in radians.

Returns the arcsine in radians.

Returns the arctangent in radians.

Returns the arctangent of y/x, using signs to determine the quadrant.

println(math.atan2(1.0, 1.0)) // 0.7853981633974483 (π/4)

Returns the hyperbolic sine.

Returns the hyperbolic cosine.

Returns the hyperbolic tangent.

Returns the inverse hyperbolic cosine.

Returns the inverse hyperbolic sine.

Returns the inverse hyperbolic tangent.

Returns the error function of x.

Returns the complementary error function (1 - erf(x)).

Returns the inverse of erf(x).

Returns the inverse of erfc(x).

Returns the Gamma function of x.

Returns [lgamma, sign] where lgamma is the natural log of |Γ(x)|.

var lg = math.lgamma(2.0)
println(lg[0]) // 0
println(lg[1]) // 1

Order-zero Bessel function of the first kind.

Order-one Bessel function of the first kind.

Order-n Bessel function of the first kind.

Order-zero Bessel function of the second kind.

Order-one Bessel function of the second kind.

Order-n Bessel function of the second kind.

math.fma(x: float, y: float, z: float): float

Section titled “math.fma(x: float, y: float, z: float): float”

Fused multiply-add: returns x*y + z computed with only one rounding.

Returns the floating-point remainder of x/y.

Returns the IEEE 754 remainder of x/y.

Returns [integer, fractional] parts of x.

var mf = math.modf(3.75)
println(mf[0]) // 3
println(mf[1]) // 0.75

Returns the next representable float64 value after x towards y.

math.nextafter32(x: float, y: float): float

Section titled “math.nextafter32(x: float, y: float): float”

Returns the next representable float32 value after x towards y.

Reports whether x is infinity. sign > 0 tests for +Inf, sign < 0 for -Inf, sign == 0 for either.

Reports whether x is NaN.

println(math.isInf(math.inf(1), 0)) // true
println(math.isNaN(math.nan())) // true

Returns the IEEE 754 binary representation of a float32 as an integer.

Returns the float32 from its IEEE 754 binary representation.

Returns the IEEE 754 binary representation of a float64 as an integer.

Returns the float64 from its IEEE 754 binary representation.

FunctionDescription
Constants
math.PI()Pi constant
math.E()Euler’s number
math.inf(sign)Positive/negative infinity
math.nan()NaN value
Basic
math.abs(x)Absolute value
math.ceil(x)Round up
math.floor(x)Round down
math.round(x)Round to nearest integer
math.roundToEven(x)Round ties to even
math.trunc(x)Truncate toward zero
math.min(a, b)Minimum
math.max(a, b)Maximum
math.dim(x, y)max(x-y, 0)
math.copysign(f, sign)Copy sign
math.signbit(x)Sign bit check
math.random()Random float 0.0–1.0
Powers & Roots
math.sqrt(x)Square root
math.cbrt(x)Cube root
math.pow(base, exp)Exponentiation
math.pow10(n)10^n
math.hypot(p, q)Hypotenuse
Exponential & Log
math.exp(x)e^x
math.exp2(x)2^x
math.expm1(x)e^x - 1
math.log(x)Natural log
math.log1p(x)log(1+x)
math.log2(x)Base-2 log
math.log10(x)Base-10 log
math.logb(x)Binary exponent (float)
math.ilogb(x)Binary exponent (int)
math.ldexp(frac, exp)frac × 2^exp
math.frexp(x)[frac, exp] decomposition
Trigonometric
math.sin(x)Sine
math.cos(x)Cosine
math.tan(x)Tangent
math.sincos(x)[sin, cos]
math.acos(x)Arccosine
math.asin(x)Arcsine
math.atan(x)Arctangent
math.atan2(y, x)Two-argument arctangent
Hyperbolic
math.sinh(x)Hyperbolic sine
math.cosh(x)Hyperbolic cosine
math.tanh(x)Hyperbolic tangent
math.acosh(x)Inverse hyperbolic cosine
math.asinh(x)Inverse hyperbolic sine
math.atanh(x)Inverse hyperbolic tangent
Error Functions
math.erf(x)Error function
math.erfc(x)Complementary error function
math.erfinv(x)Inverse error function
math.erfcinv(x)Inverse complementary error
Gamma & Bessel
math.gamma(x)Gamma function
math.lgamma(x)[log|Γ(x)|, sign]
math.j0(x)Bessel J₀
math.j1(x)Bessel J₁
math.jn(n, x)Bessel Jₙ
math.y0(x)Bessel Y₀
math.y1(x)Bessel Y₁
math.yn(n, x)Bessel Yₙ
Float Manipulation
math.fma(x, y, z)Fused multiply-add
math.mod(x, y)Float remainder
math.remainder(x, y)IEEE 754 remainder
math.modf(x)[integer, fractional]
math.nextafter(x, y)Next float64 toward y
math.nextafter32(x, y)Next float32 toward y
Classification
math.isInf(x, sign)Infinity test
math.isNaN(x)NaN test
Bit-level
math.float32bits(f)float32 → uint32 bits
math.float32frombits(b)uint32 bits → float32
math.float64bits(f)float64 → uint64 bits
math.float64frombits(b)uint64 bits → float64