Skip to content

Math Module

The std/math module provides common mathematical functions and constants.

import { math } from "std/math"

Returns the absolute value of a number.

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

Rounds up to the nearest integer.

println(math.ceil(4.1)) // 5
println(math.ceil(-2.9)) // -2

Rounds down to the nearest integer.

println(math.floor(4.9)) // 4
println(math.floor(-2.1)) // -3

Rounds to the nearest integer.

println(math.round(4.5)) // 5
println(math.round(4.4)) // 4

Returns the square root.

println(math.sqrt(16)) // 4
println(math.sqrt(2)) // 1.4142135623730951

math.pow(base: number, exp: number): number

Section titled “math.pow(base: number, exp: number): number”

Returns base raised to the power of exp.

println(math.pow(2, 10)) // 1024
println(math.pow(3, 3)) // 27

Returns the smaller of two values.

println(math.min(5, 3)) // 3

Returns the larger of two values.

println(math.max(5, 3)) // 5

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

const val = math.random() * 100
const rounded = math.floor(val)
println(rounded) // Random integer 0-99

Returns the sine of x (in radians).

Returns the cosine of x (in radians).

Returns the tangent of x (in radians).

println(math.sin(0)) // 0
println(math.cos(0)) // 1
println(math.tan(math.pi() / 4)) // ~1

Returns the natural logarithm (base e) of x.

Returns the base-10 logarithm of x.

println(math.log(math.e())) // 1
println(math.log10(1000)) // 3

Returns Pi (3.14159…).

Returns Euler’s number (2.71828…).

println(math.pi()) // 3.141592653589793
println(math.e()) // 2.718281828459045
FunctionDescription
math.abs(x)Absolute value
math.ceil(x)Round up
math.floor(x)Round down
math.round(x)Round to nearest integer
math.sqrt(x)Square root
math.pow(base, exp)Exponentiation
math.min(a, b)Minimum of two values
math.max(a, b)Maximum of two values
math.random()Random float 0.0–1.0
math.sin(x)Sine (radians)
math.cos(x)Cosine (radians)
math.tan(x)Tangent (radians)
math.log(x)Natural logarithm
math.log10(x)Base-10 logarithm
math.pi()Pi constant
math.e()Euler’s number