Math
All Math methods are static, just like JavaScript. They compile to Rust's built-in numeric methods.
Rounding
Math.floor(3.7); // 3.0
Math.ceil(3.2); // 4.0
Math.round(3.5); // 4.0
Math.trunc(3.9); // 3.0
Absolute value
Math.abs(-42.0); // 42.0
Powers and roots
Math.sqrt(16.0); // 4.0
Math.cbrt(27.0); // 3.0
Math.pow(2.0, 10.0); // 1024.0
Math.hypot(3.0, 4.0); // 5.0
Min / Max
Math.min(3.0, 7.0); // 3.0
Math.max(3.0, 7.0); // 7.0
Random
const r = Math.random(); // Random f64 in [0, 1)
Logarithms
Math.log(2.718); // ~1.0 (natural log)
Math.log2(8.0); // 3.0
Math.log10(1000.0); // 3.0
Math.log1p(0.0); // 0.0 (ln(1 + x), accurate for small x)
Trigonometry
Math.sin(0.0); // 0.0
Math.cos(0.0); // 1.0
Math.tan(0.0); // 0.0
Math.asin(1.0); // ~1.5708 (PI/2)
Math.acos(1.0); // 0.0
Math.atan(1.0); // ~0.7854 (PI/4)
Math.atan2(1.0, 1.0); // ~0.7854
Hyperbolic
Math.sinh(1.0);
Math.cosh(1.0);
Math.tanh(1.0);
Math.asinh(1.0);
Math.acosh(1.0);
Math.atanh(0.5);
Exponential
Math.exp(1.0); // ~2.718
Math.expm1(0.0); // 0.0 (e^x - 1, accurate for small x)
Other
Math.sign(42.0); // 1.0
Math.sign(-42.0); // -1.0
Math.fround(3.14); // Rounds to f32 precision
Math.clz32(1); // 31 (count leading zeros in 32-bit)
Constants
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045
Math.LN2; // 0.6931471805599453
Math.LN10; // 2.302585092994046
Math.LOG2E; // 1.4426950408889634
Math.LOG10E; // 0.4342944819032518
Math.SQRT2; // 1.4142135623730951
Math.SQRT1_2; // 0.7071067811865476
How it compiles
Math methods compile to Rust's method syntax on the numeric type:
| RustScript | Rust |
|-----------|------|
| Math.floor(x) | x.floor() |
| Math.abs(x) | x.abs() |
| Math.sqrt(x) | x.sqrt() |
| Math.pow(x, y) | x.powf(y) |
| Math.min(a, b) | a.min(b) |
| Math.max(a, b) | a.max(b) |
| Math.random() | Random via rand crate |