Functions
The circle constant π ≈ 3.141592653589793.
println(std.math.pi()) // 3.141592653589793
println(std.math.tau()) // 6.283185307179586
println(std.math.e()) // 2.718281828459045Euler's number e ≈ 2.718281828459045.
The full-turn constant τ = 2π ≈ 6.283185307179586.
Square root of x.
x: Radicand. Negative input yields NaN.
println(std.math.sqrt(16.0)) // 4
println(std.math.sqrt(2.0)) // 1.4142135623730951Largest 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.5Smallest integral value ≥ x.
x: Value to round up.
x rounded to the nearest integral value (half away from zero).
x: Value to round to the nearest integral float.
Absolute value of a float.
x: Value whose magnitude is returned.
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 — π/4Cosine of x (radians).
x: Angle in radians.
Tangent of x (radians).
x: Angle in radians.
Arc sine of x, in radians.
x: Sine value in [-1, 1]; anything outside yields NaN.
Arc cosine of x, in radians.
x: Cosine value in [-1, 1]; anything outside yields NaN.
Arc tangent of x, in radians.
x: Tangent value; the result lies in (-π/2, π/2).
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.7182818284590455Natural 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)) // 3Base-2 logarithm of x.
x: Positive value; zero yields -inf and negatives NaN.
Base-10 logarithm of x.
x: Positive value; zero yields -inf and negatives NaN.
Radians for the degree angle x.
x: Angle in degrees.
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.141592653589793Truncate 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 conversionFloating-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.5Arc 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 — -π/2sqrt(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)) // 5Smaller of two floats.
a: First candidate.b: Second candidate.
Larger of two floats.
a: First candidate.b: Second candidate.
x clamped to the inclusive range [lo, hi] (floats).
x: Value to constrain.lo: Lower bound, returned whenxfalls below it.hi: Upper bound, returned whenxrises 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.5Linear interpolation: x + (y - x) * t.
x: Value returned att == 0.0.y: Value returned att == 1.0.t: Interpolation fraction; values outside [0, 1] extrapolate.
println(std.math.lerp(0.0, 10.0, 0.25)) // 2.5Convert an int to a float.
x: Integer to widen.
Convert an int to a float (alias of as_float).
x: Integer to widen.
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)) // 0Returns -1, 0, or 1 according to the integer sign.
x: Value whose sign is reported.
Returns the factorial of a non-negative integer.
n: Non-negative count; anything below 2 returns 1.
println(std.math.factorial(5)) // 120Returns the requested Fibonacci number.
n: Zero-based index into the sequence 0, 1, 1, 2, 3, …
println(std.math.fibonacci(10)) // 55Returns the smaller value.
a: First candidate.b: Second candidate.
println(std.math.min(3, 9)) // 3
println(std.math.max(3, 9)) // 9Returns the larger value.
a: First candidate.b: Second candidate.
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)) // 12Returns the least common multiple.
x: First value.y: Second value. Zero on either side returns 0.
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)) // 1024Returns 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)) // 55Clamps a value to the inclusive bounds.
x: Value to constrain.lo: Lower bound, returned whenxfalls below it.hi: Upper bound, returned whenxrises above it.
println(std.math.clamp(15, 0, 10)) // 10
println(std.math.clamp(-2, 0, 10)) // 0Reports whether the integer is even.
x: Value to test.
println(std.math.is_even(4)) // true
println(std.math.is_odd(4)) // falseReports whether the integer is odd.
x: Value to test.
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