Skip to content

Math

The global Math object is available in every program without an import, just like console. It exposes the constants and floating-point functions you know from JavaScript. Every method operates on f64.

console.log("The square root of 16 is 4");
if (Math.sqrt(16.0) == 4.0) {
console.log("correct!");
}

Constants

ConstantValueDescription
Math.PI3.141592653589793Ratio of a circle’s circumference to its diameter.
Math.E2.718281828459045Euler’s number, the base of natural logarithms.
if (Math.PI > 3.14 && Math.PI < 3.15) {
console.log("PI looks right");
}

Rounding

Math.floor(x: f64): f64 // largest integer <= x
Math.ceil(x: f64): f64 // smallest integer >= x
Math.round(x: f64): f64 // nearest integer, halves away from zero
Math.trunc(x: f64): f64 // drops the fractional part
Math.floor(3.7); // 3.0
Math.ceil(3.2); // 4.0
Math.round(3.5); // 4.0
Math.trunc(3.9); // 3.0

Powers, roots, and logarithms

Math.pow(x: f64, y: f64): f64 // x raised to the power y
Math.sqrt(x: f64): f64 // square root
Math.cbrt(x: f64): f64 // cube root
Math.exp(x: f64): f64 // e raised to the power x
Math.log(x: f64): f64 // natural logarithm (base e)
Math.log2(x: f64): f64 // base-2 logarithm
Math.log10(x: f64): f64 // base-10 logarithm
Math.pow(2.0, 10.0); // 1024.0
Math.sqrt(144.0); // 12.0
Math.log2(8.0); // 3.0

Trigonometry

Math.sin(x: f64): f64 // sine of x (radians)
Math.cos(x: f64): f64 // cosine of x (radians)
Math.tan(x: f64): f64 // tangent of x (radians)
Math.sin(0.0); // 0.0
Math.cos(0.0); // 1.0
Math.cos(Math.PI); // -1.0

Sign, absolute value, and comparison

Math.abs(x: f64): f64 // absolute value
Math.sign(x: f64): f64 // -1.0, 0.0, or 1.0
Math.min(a: f64, b: f64): f64 // smaller of a and b
Math.max(a: f64, b: f64): f64 // larger of a and b
Math.hypot(a: f64, b: f64): f64 // sqrt(a*a + b*b)
Math.abs(-5.0); // 5.0
Math.sign(-3.0); // -1.0
Math.max(2.0, 9.0); // 9.0
Math.hypot(3.0, 4.0); // 5.0

Random

Math.random() returns a uniformly distributed f64 in the range [0.0, 1.0).

let r: f64 = Math.random(); // e.g. 0.4271...