Math Module
The std/math module provides common mathematical functions and constants.
Import
Section titled “Import”import { math } from "std/math"Rounding & Absolute Value
Section titled “Rounding & Absolute Value”math.abs(n: number): number
Section titled “math.abs(n: number): number”Returns the absolute value of a number.
println(math.abs(-5)) // 5println(math.abs(3.7)) // 3.7math.ceil(n: number): int
Section titled “math.ceil(n: number): int”Rounds up to the nearest integer.
println(math.ceil(4.1)) // 5println(math.ceil(-2.9)) // -2math.floor(n: number): int
Section titled “math.floor(n: number): int”Rounds down to the nearest integer.
println(math.floor(4.9)) // 4println(math.floor(-2.1)) // -3math.round(n: number): int
Section titled “math.round(n: number): int”Rounds to the nearest integer.
println(math.round(4.5)) // 5println(math.round(4.4)) // 4Arithmetic
Section titled “Arithmetic”math.sqrt(x: number): number
Section titled “math.sqrt(x: number): number”Returns the square root.
println(math.sqrt(16)) // 4println(math.sqrt(2)) // 1.4142135623730951math.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)) // 1024println(math.pow(3, 3)) // 27math.min(a: number, b: number): number
Section titled “math.min(a: number, b: number): number”Returns the smaller of two values.
println(math.min(5, 3)) // 3math.max(a: number, b: number): number
Section titled “math.max(a: number, b: number): number”Returns the larger of two values.
println(math.max(5, 3)) // 5Random
Section titled “Random”math.random(): float
Section titled “math.random(): float”Returns a random float between 0.0 (inclusive) and 1.0 (exclusive).
const val = math.random() * 100const rounded = math.floor(val)println(rounded) // Random integer 0-99Trigonometric
Section titled “Trigonometric”math.sin(x: number): number
Section titled “math.sin(x: number): number”Returns the sine of x (in radians).
math.cos(x: number): number
Section titled “math.cos(x: number): number”Returns the cosine of x (in radians).
math.tan(x: number): number
Section titled “math.tan(x: number): number”Returns the tangent of x (in radians).
println(math.sin(0)) // 0println(math.cos(0)) // 1println(math.tan(math.pi() / 4)) // ~1Logarithmic
Section titled “Logarithmic”math.log(x: number): number
Section titled “math.log(x: number): number”Returns the natural logarithm (base e) of x.
math.log10(x: number): number
Section titled “math.log10(x: number): number”Returns the base-10 logarithm of x.
println(math.log(math.e())) // 1println(math.log10(1000)) // 3Constants
Section titled “Constants”math.pi(): float
Section titled “math.pi(): float”Returns Pi (3.14159…).
math.e(): float
Section titled “math.e(): float”Returns Euler’s number (2.71828…).
println(math.pi()) // 3.141592653589793println(math.e()) // 2.718281828459045Function Reference
Section titled “Function Reference”| Function | Description |
|---|---|
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 |