std.math

Numeric helpers, roots, powers, trigonometry, and logarithms.

Importuse std.math

Functions

extern fn pi() -> float#

The circle constant π ≈ 3.141592653589793.

println(std.math.pi())  // 3.141592653589793
println(std.math.tau()) // 6.283185307179586
println(std.math.e())   // 2.718281828459045
extern fn e() -> float#

Euler's number e ≈ 2.718281828459045.

extern fn tau() -> float#

The full-turn constant τ = 2π ≈ 6.283185307179586.

extern fn sqrt(x: float) -> float#

Square root of x.

  • x: Radicand. Negative input yields NaN.
println(std.math.sqrt(16.0)) // 4
println(std.math.sqrt(2.0))  // 1.4142135623730951
extern fn floor(x: float) -> float#

Largest integral value ≤ x.

  • x: Value to round down.
println(std.math.floor(2.7))      // 2
println(std.math.ceil(2.1))       // 3
println(std.math.round(2.5))      // 3
println(std.math.round(-2.5))     // -3 — half away from zero
println(std.math.abs_float(-2.5)) // 2.5
extern fn ceil(x: float) -> float#

Smallest integral value ≥ x.

  • x: Value to round up.
extern fn round(x: float) -> float#

x rounded to the nearest integral value (half away from zero).

  • x: Value to round to the nearest integral float.
extern fn abs_float(x: float) -> float#

Absolute value of a float.

  • x: Value whose magnitude is returned.
extern fn sin(x: float) -> float#

Sine of x (radians).

  • x: Angle in radians.
println(std.math.sin(std.math.pi() / 2.0)) // 1
println(std.math.cos(0.0))                 // 1
println(std.math.atan(1.0))                // 0.7853981633974483 — π/4
extern fn cos(x: float) -> float#

Cosine of x (radians).

  • x: Angle in radians.
extern fn tan(x: float) -> float#

Tangent of x (radians).

  • x: Angle in radians.
extern fn asin(x: float) -> float#

Arc sine of x, in radians.

  • x: Sine value in [-1, 1]; anything outside yields NaN.
extern fn acos(x: float) -> float#

Arc cosine of x, in radians.

  • x: Cosine value in [-1, 1]; anything outside yields NaN.
extern fn atan(x: float) -> float#

Arc tangent of x, in radians.

  • x: Tangent value; the result lies in (-π/2, π/2).
extern fn exp(x: float) -> float#

e raised to the power x.

  • x: Exponent to raise e to.
println(std.math.exp(0.0)) // 1
println(std.math.exp(1.0)) // 2.7182818284590455
extern fn log(x: float) -> float#

Natural logarithm of x.

  • x: Positive value; zero yields -inf and negatives NaN.
println(std.math.log(std.math.e())) // 1
println(std.math.log2(8.0))         // 3
println(std.math.log10(1000.0))     // 3
extern fn log2(x: float) -> float#

Base-2 logarithm of x.

  • x: Positive value; zero yields -inf and negatives NaN.
extern fn log10(x: float) -> float#

Base-10 logarithm of x.

  • x: Positive value; zero yields -inf and negatives NaN.
extern fn to_radians(x: float) -> float#

Radians for the degree angle x.

  • x: Angle in degrees.
extern fn to_degrees(x: float) -> float#

Degrees for the radian angle x.

  • x: Angle in radians.
println(std.math.to_degrees(std.math.pi())) // 180
println(std.math.to_radians(180.0))         // 3.141592653589793
extern fn as_int(x: float) -> int#

Truncate a float toward zero to an int.

  • x: Float to truncate; the fractional part is discarded.
println(std.math.as_int(2.9))  // 2
println(std.math.as_int(-2.9)) // -2
println(std.math.as_float(3))  // 3 — `int_to_float` is the same conversion
extern fn pow(base: float, exp: float) -> float#

base raised to the power exp (float).

  • base: Value to raise.
  • exp: Exponent, which may be fractional or negative.
println(std.math.pow(2.0, 10.0)) // 1024
println(std.math.pow(9.0, 0.5))  // 3
extern fn fmod(x: float, y: float) -> float#

Floating-point remainder of x / y (same sign as x).

  • x: Dividend, and the sign the result carries.
  • y: Divisor; zero yields NaN.
println(std.math.fmod(7.5, 2.0))  // 1.5
println(std.math.fmod(-7.5, 2.0)) // -1.5
extern fn atan2(y: float, x: float) -> float#

Arc tangent of y/x using the signs of both to find the quadrant.

  • y: Vertical component of the vector.
  • x: Horizontal component of the vector.
println(std.math.atan2(1.0, 1.0))  // 0.7853981633974483 — π/4
println(std.math.atan2(-1.0, 0.0)) // -1.5707963267948966 — -π/2
extern fn hypot(x: float, y: float) -> float#

sqrt(x*x + y*y) without undue overflow.

  • x: One leg of the right triangle.
  • y: The other leg.
println(std.math.hypot(3.0, 4.0)) // 5
extern fn min_float(a: float, b: float) -> float#

Smaller of two floats.

  • a: First candidate.
  • b: Second candidate.
extern fn max_float(a: float, b: float) -> float#

Larger of two floats.

  • a: First candidate.
  • b: Second candidate.
extern fn clamp_float(x: float, lo: float, hi: float) -> float#

x clamped to the inclusive range [lo, hi] (floats).

  • x: Value to constrain.
  • lo: Lower bound, returned when x falls below it.
  • hi: Upper bound, returned when x rises above it.
println(std.math.clamp_float(3.7, 0.0, 1.0)) // 1
println(std.math.min_float(1.5, 2.5))        // 1.5
println(std.math.max_float(1.5, 2.5))        // 2.5
extern fn lerp(x: float, y: float, t: float) -> float#

Linear interpolation: x + (y - x) * t.

  • x: Value returned at t == 0.0.
  • y: Value returned at t == 1.0.
  • t: Interpolation fraction; values outside [0, 1] extrapolate.
println(std.math.lerp(0.0, 10.0, 0.25)) // 2.5
extern fn as_float(x: int) -> float#

Convert an int to a float.

  • x: Integer to widen.
extern fn int_to_float(x: int) -> float#

Convert an int to a float (alias of as_float).

  • x: Integer to widen.
fn abs(x: int) -> int#

Returns the absolute value.

  • x: Value whose magnitude is returned.
println(std.math.abs(-7))  // 7
println(std.math.sign(-7)) // -1
println(std.math.sign(0))  // 0
fn sign(x: int) -> int#

Returns -1, 0, or 1 according to the integer sign.

  • x: Value whose sign is reported.
fn factorial(n: int) -> int#

Returns the factorial of a non-negative integer.

  • n: Non-negative count; anything below 2 returns 1.
println(std.math.factorial(5)) // 120
fn fibonacci(n: int) -> int#

Returns the requested Fibonacci number.

  • n: Zero-based index into the sequence 0, 1, 1, 2, 3, …
println(std.math.fibonacci(10)) // 55
fn min(a: int, b: int) -> int#

Returns the smaller value.

  • a: First candidate.
  • b: Second candidate.
println(std.math.min(3, 9)) // 3
println(std.math.max(3, 9)) // 9
fn max(a: int, b: int) -> int#

Returns the larger value.

  • a: First candidate.
  • b: Second candidate.
fn gcd(a: int, b: int) -> int#

Returns the greatest common divisor.

  • a: First value; its sign is ignored.
  • b: Second value; its sign is ignored.
println(std.math.gcd(12, 18)) // 6
println(std.math.lcm(4, 6))   // 12
fn lcm(x: int, y: int) -> int#

Returns the least common multiple.

  • x: First value.
  • y: Second value. Zero on either side returns 0.
fn pow_int(base: int, exp: int) -> int#

Raises an integer base to a non-negative integer exponent.

  • base: Value to raise; the product wraps on overflow.
  • exp: Non-negative exponent; a negative one returns 1.
println(std.math.pow_int(2, 10)) // 1024
fn sum_range(start: int, end: int) -> int#

Returns the sum of the half-open integer range.

  • start: First value included.
  • end: One past the last value included.
println(std.math.sum_range(1, 11)) // 55
fn clamp(x: int, lo: int, hi: int) -> int#

Clamps a value to the inclusive bounds.

  • x: Value to constrain.
  • lo: Lower bound, returned when x falls below it.
  • hi: Upper bound, returned when x rises above it.
println(std.math.clamp(15, 0, 10)) // 10
println(std.math.clamp(-2, 0, 10)) // 0
fn is_even(x: int) -> bool#

Reports whether the integer is even.

  • x: Value to test.
println(std.math.is_even(4)) // true
println(std.math.is_odd(4))  // false
fn is_odd(x: int) -> bool#

Reports whether the integer is odd.

  • x: Value to test.
fn is_prime(n: int) -> bool#

Reports whether the integer is prime.

  • n: Value to test; anything below 2 is not prime.
println(std.math.is_prime(13)) // true
println(std.math.is_prime(14)) // false