Math Module
The std/math module provides comprehensive mathematical functions and constants with full Go math package parity.
Import
Section titled “Import”import { math } from "std/math"Constants & Special Values
Section titled “Constants & Special Values”math.PI(): float
Section titled “math.PI(): float”Returns Pi (3.14159…).
math.E(): float
Section titled “math.E(): float”Returns Euler’s number (2.71828…).
math.inf(sign: int): float
Section titled “math.inf(sign: int): float”Returns positive infinity if sign >= 0, negative infinity if sign < 0.
math.nan(): float
Section titled “math.nan(): float”Returns an IEEE 754 “not-a-number” value.
println(math.PI()) // 3.141592653589793println(math.E()) // 2.718281828459045println(math.inf(1)) // +Infprintln(math.nan()) // NaNmath.abs(n: float): float
Section titled “math.abs(n: float): float”Returns the absolute value of a number.
println(math.abs(-5.0)) // 5println(math.abs(3.7)) // 3.7math.ceil(n: float): int
Section titled “math.ceil(n: float): int”Rounds up to the nearest integer.
math.floor(n: float): int
Section titled “math.floor(n: float): int”Rounds down to the nearest integer.
math.round(n: float): int
Section titled “math.round(n: float): int”Rounds to the nearest integer.
math.roundToEven(x: float): float
Section titled “math.roundToEven(x: float): float”Rounds to the nearest integer, rounding ties to even.
println(math.roundToEven(0.5)) // 0println(math.roundToEven(1.5)) // 2println(math.roundToEven(2.5)) // 2math.trunc(x: float): float
Section titled “math.trunc(x: float): float”Returns the integer value of x (truncates toward zero).
println(math.trunc(2.7)) // 2println(math.trunc(-2.7)) // -2math.min(a: float, b: float): float
Section titled “math.min(a: float, b: float): float”Returns the smaller of two values.
math.max(a: float, b: float): float
Section titled “math.max(a: float, b: float): float”Returns the larger of two values.
math.dim(x: float, y: float): float
Section titled “math.dim(x: float, y: float): float”Returns max(x - y, 0).
println(math.dim(5.0, 3.0)) // 2println(math.dim(3.0, 5.0)) // 0math.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.
math.signbit(x: float): bool
Section titled “math.signbit(x: float): bool”Reports whether the sign bit of x is set (negative or negative zero).
math.random(): float
Section titled “math.random(): float”Returns a random float between 0.0 (inclusive) and 1.0 (exclusive).
Powers & Roots
Section titled “Powers & Roots”math.sqrt(x: float): float
Section titled “math.sqrt(x: float): float”Returns the square root.
math.cbrt(x: float): float
Section titled “math.cbrt(x: float): float”Returns the cube root.
println(math.cbrt(27.0)) // 3println(math.cbrt(8.0)) // 2math.pow(base: float, exp: float): float
Section titled “math.pow(base: float, exp: float): float”Returns base raised to the power of exp.
math.pow10(n: int): float
Section titled “math.pow10(n: int): float”Returns 10 raised to the power of n.
println(math.pow10(3)) // 1000math.hypot(p: float, q: float): float
Section titled “math.hypot(p: float, q: float): float”Returns sqrt(p*p + q*q), taking care not to overflow or underflow.
println(math.hypot(3.0, 4.0)) // 5Exponential & Logarithmic
Section titled “Exponential & Logarithmic”math.exp(x: float): float
Section titled “math.exp(x: float): float”Returns e**x.
math.exp2(x: float): float
Section titled “math.exp2(x: float): float”Returns 2**x.
math.expm1(x: float): float
Section titled “math.expm1(x: float): float”Returns e**x - 1, more accurate than exp(x) - 1 when x is near zero.
math.log(x: float): float
Section titled “math.log(x: float): float”Returns the natural logarithm (base e).
math.log1p(x: float): float
Section titled “math.log1p(x: float): float”Returns log(1 + x), more accurate when x is near zero.
math.log2(x: float): float
Section titled “math.log2(x: float): float”Returns the base-2 logarithm.
math.log10(x: float): float
Section titled “math.log10(x: float): float”Returns the base-10 logarithm.
math.logb(x: float): float
Section titled “math.logb(x: float): float”Returns the binary exponent of x as a float.
math.ilogb(x: float): int
Section titled “math.ilogb(x: float): int”Returns the binary exponent of x as an integer.
math.ldexp(frac: float, exp: int): float
Section titled “math.ldexp(frac: float, exp: int): float”Returns frac × 2**exp (inverse of frexp).
math.frexp(x: float): []any
Section titled “math.frexp(x: float): []any”Returns [frac, exp] such that x == frac × 2**exp.
var result = math.frexp(4.0)println(result[0]) // 0.5println(result[1]) // 3Trigonometric
Section titled “Trigonometric”math.sin(x: float): float
Section titled “math.sin(x: float): float”Returns the sine of x (radians).
math.cos(x: float): float
Section titled “math.cos(x: float): float”Returns the cosine of x (radians).
math.tan(x: float): float
Section titled “math.tan(x: float): float”Returns the tangent of x (radians).
math.sincos(x: float): []any
Section titled “math.sincos(x: float): []any”Returns [sin(x), cos(x)].
var sc = math.sincos(0.0)println(sc[0]) // 0 (sin)println(sc[1]) // 1 (cos)Inverse Trigonometric
Section titled “Inverse Trigonometric”math.acos(x: float): float
Section titled “math.acos(x: float): float”Returns the arccosine in radians.
math.asin(x: float): float
Section titled “math.asin(x: float): float”Returns the arcsine in radians.
math.atan(x: float): float
Section titled “math.atan(x: float): float”Returns the arctangent in radians.
math.atan2(y: float, x: float): float
Section titled “math.atan2(y: float, x: float): float”Returns the arctangent of y/x, using signs to determine the quadrant.
println(math.atan2(1.0, 1.0)) // 0.7853981633974483 (π/4)Hyperbolic
Section titled “Hyperbolic”math.sinh(x: float): float
Section titled “math.sinh(x: float): float”Returns the hyperbolic sine.
math.cosh(x: float): float
Section titled “math.cosh(x: float): float”Returns the hyperbolic cosine.
math.tanh(x: float): float
Section titled “math.tanh(x: float): float”Returns the hyperbolic tangent.
Inverse Hyperbolic
Section titled “Inverse Hyperbolic”math.acosh(x: float): float
Section titled “math.acosh(x: float): float”Returns the inverse hyperbolic cosine.
math.asinh(x: float): float
Section titled “math.asinh(x: float): float”Returns the inverse hyperbolic sine.
math.atanh(x: float): float
Section titled “math.atanh(x: float): float”Returns the inverse hyperbolic tangent.
Error Functions
Section titled “Error Functions”math.erf(x: float): float
Section titled “math.erf(x: float): float”Returns the error function of x.
math.erfc(x: float): float
Section titled “math.erfc(x: float): float”Returns the complementary error function (1 - erf(x)).
math.erfinv(x: float): float
Section titled “math.erfinv(x: float): float”Returns the inverse of erf(x).
math.erfcinv(x: float): float
Section titled “math.erfcinv(x: float): float”Returns the inverse of erfc(x).
Gamma & Bessel
Section titled “Gamma & Bessel”math.gamma(x: float): float
Section titled “math.gamma(x: float): float”Returns the Gamma function of x.
math.lgamma(x: float): []any
Section titled “math.lgamma(x: float): []any”Returns [lgamma, sign] where lgamma is the natural log of |Γ(x)|.
var lg = math.lgamma(2.0)println(lg[0]) // 0println(lg[1]) // 1math.j0(x: float): float
Section titled “math.j0(x: float): float”Order-zero Bessel function of the first kind.
math.j1(x: float): float
Section titled “math.j1(x: float): float”Order-one Bessel function of the first kind.
math.jn(n: int, x: float): float
Section titled “math.jn(n: int, x: float): float”Order-n Bessel function of the first kind.
math.y0(x: float): float
Section titled “math.y0(x: float): float”Order-zero Bessel function of the second kind.
math.y1(x: float): float
Section titled “math.y1(x: float): float”Order-one Bessel function of the second kind.
math.yn(n: int, x: float): float
Section titled “math.yn(n: int, x: float): float”Order-n Bessel function of the second kind.
Floating-point Manipulation
Section titled “Floating-point Manipulation”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.
math.mod(x: float, y: float): float
Section titled “math.mod(x: float, y: float): float”Returns the floating-point remainder of x/y.
math.remainder(x: float, y: float): float
Section titled “math.remainder(x: float, y: float): float”Returns the IEEE 754 remainder of x/y.
math.modf(x: float): []any
Section titled “math.modf(x: float): []any”Returns [integer, fractional] parts of x.
var mf = math.modf(3.75)println(mf[0]) // 3println(mf[1]) // 0.75math.nextafter(x: float, y: float): float
Section titled “math.nextafter(x: float, y: float): float”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.
Classification
Section titled “Classification”math.isInf(x: float, sign: int): bool
Section titled “math.isInf(x: float, sign: int): bool”Reports whether x is infinity. sign > 0 tests for +Inf, sign < 0 for -Inf, sign == 0 for either.
math.isNaN(x: float): bool
Section titled “math.isNaN(x: float): bool”Reports whether x is NaN.
println(math.isInf(math.inf(1), 0)) // trueprintln(math.isNaN(math.nan())) // trueBit-level Conversions
Section titled “Bit-level Conversions”math.float32bits(f: float): int
Section titled “math.float32bits(f: float): int”Returns the IEEE 754 binary representation of a float32 as an integer.
math.float32frombits(b: int): float
Section titled “math.float32frombits(b: int): float”Returns the float32 from its IEEE 754 binary representation.
math.float64bits(f: float): int
Section titled “math.float64bits(f: float): int”Returns the IEEE 754 binary representation of a float64 as an integer.
math.float64frombits(b: int): float
Section titled “math.float64frombits(b: int): float”Returns the float64 from its IEEE 754 binary representation.
Function Reference
Section titled “Function Reference”| Function | Description |
|---|---|
| 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 |