std / math

math

import "std:math"

Provides mathematical functions and constants. Includes integer and floating-point arithmetic, trigonometry, logarithms, rounding, interpolation, and IEEE 754 classification functions.

View source on Codeberg →

Constants

#
const (
    E          = 2.71828182845904523536
    Pi         = 3.14159265358979323846
    Phi        = 1.61803398874989484820
    Sqrt2      = 1.41421356237309504880
    SqrtE      = 1.64872127070012814685
    SqrtPi     = 1.77245385090551602730
    Ln2        = 0.693147180559945309417
    Log2E      = 1.44269504088896340736
    Ln10       = 2.30258509299404568402
    Log10E     = 0.434294481903251827651
    MaxFloat64             = 1.7976931348623157e+308
    SmallestNonzeroFloat64 = 1.0e-200
    MaxInt  = 9223372036854775807
    MinInt  = -9223372036854775808
    DegToRad = Pi / 180.0
    RadToDeg = 180.0 / Pi
)

Mathematical constants including Euler's number (E), Pi, the golden ratio (Phi), common square roots, logarithmic values, floating-point limits, integer limits, and angle conversion factors.

Integer Functions

#
func Abs(x int) int

Returns the absolute value of an integer.

#
func Min(a, b int) int

Returns the smaller of two integers.

#
func Max(a, b int) int

Returns the larger of two integers.

#
func Clamp(x, lo, hi int) int

Restricts x to be within the range [lo, hi].

#
func Sign(x int) int

Returns -1 if x < 0, 0 if x == 0, or 1 if x > 0.

Float Comparison

#
func MinFloat(a, b float64) float64

Returns the smaller of two floats.

#
func MaxFloat(a, b float64) float64

Returns the larger of two floats.

#
func ClampFloat(x, lo, hi float64) float64

Restricts x to be within the range [lo, hi].

#
func SignFloat(x float64) float64

Returns -1.0, 0.0, or 1.0 based on the sign of x.

#
func Dim(x, y float64) float64

Returns max(x-y, 0).

Integer Algorithms

#
func Gcd(a, b int) int

Returns the greatest common divisor of a and b.

#
func Lcm(a, b int) int

Returns the least common multiple of a and b.

#
func Factorial(n int) int

Returns n! for n in [0, 20]. Returns 0 for out-of-range.

#
func IsPowerOfTwo(x int) bool

Returns true if x is a power of two.

Float Classification

#
func IsNaN(x float64) bool

Reports whether x is NaN.

#
func IsInf(x float64, sign int) bool

Reports whether x is infinity.

#
func NaN() float64

Returns a NaN value.

#
func Inf(sign int) float64

Returns positive or negative infinity.

Interpolation

#
func Lerp(a, b, t float64) float64

Returns the linear interpolation between a and b.

#
func InverseLerp(a, b, v float64) float64

Returns where v falls in range [a, b].

#
func SmoothStep(e0, e1, x float64) float64

Performs Hermite interpolation between 0 and 1.

Angle Conversion

#
func Degrees(rad float64) float64

Converts radians to degrees.

#
func Radians(deg float64) float64

Converts degrees to radians.

Basic Math

#
func Sqrt(x float64) float64

Returns the square root of x.

#
func Pow(x, y float64) float64

Returns x**y.

#
func Fabs(x float64) float64

Returns the absolute value of x.

#
func Fmod(x, y float64) float64

Returns the floating-point remainder of x/y.

Exponential and Logarithmic

#
func Exp(x float64) float64

Returns e**x.

#
func Log(x float64) float64

Returns the natural logarithm of x.

#
func Log10(x float64) float64

Returns the base-10 logarithm of x.

#
func Exp2(x float64) float64

Returns 2**x.

#
func Log2(x float64) float64

Returns the base-2 logarithm of x.

#
func Log1p(x float64) float64

Returns log(1+x) accurately for small x.

#
func Expm1(x float64) float64

Returns exp(x)-1 accurately for small x.

Rounding

#
func Floor(x float64) float64

Returns the greatest integer value less than or equal to x.

#
func Ceil(x float64) float64

Returns the least integer value greater than or equal to x.

#
func Round(x float64) float64

Returns the nearest integer, rounding half away from zero.

#
func Trunc(x float64) float64

Returns the integer value of x, truncated toward zero.

Trigonometric

#
func Sin(x float64) float64

Returns the sine of x (radians).

#
func Cos(x float64) float64

Returns the cosine of x (radians).

#
func Tan(x float64) float64

Returns the tangent of x (radians).

#
func Sincos(x float64) (float64, float64)

Returns both Sin(x) and Cos(x).

Inverse Trigonometric

#
func Asin(x float64) float64

Returns the arcsine of x (radians).

#
func Acos(x float64) float64

Returns the arccosine of x (radians).

#
func Atan(x float64) float64

Returns the arctangent of x (radians).

#
func Atan2(y, x float64) float64

Returns the arctangent of y/x.

Hyperbolic

#
func Sinh(x float64) float64

Returns the hyperbolic sine of x.

#
func Cosh(x float64) float64

Returns the hyperbolic cosine of x.

#
func Tanh(x float64) float64

Returns the hyperbolic tangent of x.

#
func Asinh(x float64) float64

Returns the inverse hyperbolic sine of x.

#
func Acosh(x float64) float64

Returns the inverse hyperbolic cosine of x.

#
func Atanh(x float64) float64

Returns the inverse hyperbolic tangent of x.

Float Manipulation

#
func Frexp(x float64) (float64, int)

Breaks x into normalized fraction and power of 2.

#
func Ldexp(frac float64, exp int) float64

Returns frac * 2**exp.

#
func Modf(x float64) (float64, float64)

Returns integer and fractional parts of x.

#
func Cbrt(x float64) float64

Returns the cube root of x.

#
func Hypot(x, y float64) float64

Returns sqrt(x*x + y*y), avoiding overflow.

#
func Copysign(x, y float64) float64

Returns a value with magnitude of x and sign of y.