math
The math module provides mathematical functions and constants.
Import
Section titled “Import”import mathimport { sqrt, sin, cos, pi } from mathConstants
Section titled “Constants”@math#PI @// 3.14159265358979@math#E @// 2.71828182845905@math#TAU @// 6.28318530717959@math#PHI @// 1.61803398874989@math#LN2 @// 0.693147180559945@math#LN10 @// 2.30258509299405@math#SQRT2 @// 1.41421356237310@math#EPSILON @// Smallest positive Float@math#MAX_FLOAT @// Maximum Float value@math#MIN_FLOAT @// Minimum Float value@math#MAX_INT @// Maximum Int value@math#MIN_INT @// Minimum Int valueBasic Operations
Section titled “Basic Operations”Absolute value:
@math#abs(-5) @// 5@math#abs(-3.14) @// 3.14Minimum of two values:
@math#min(10, 20) @// 10@math#min(3.14, 2.71) @// 2.71Maximum of two values:
@math#max(10, 20) @// 20Clamp value between min and max:
@math#clamp(150, 0, 100) @// 100@math#clamp(-10, 0, 100) @// 0@math#clamp(50, 0, 100) @// 50Sign of a number:
@math#sign(-5) @// -1@math#sign(0) @// 0@math#sign(5) @// 1Power Functions
Section titled “Power Functions”Square root:
@math#sqrt(144) @// 12.0@math#sqrt(2) @// 1.41421356237310Cube root:
@math#cbrt(27) @// 3.0Power function:
@math#pow(2, 10) @// 1024.0@math#pow(27, 1/3) @// 3.0Exponential function (e^x):
@math#exp(1) @// 2.71828182845905@math#exp(0) @// 1.0Power of 10:
@math#pow10(3) @// 1000.0Logarithmic Functions
Section titled “Logarithmic Functions”Natural logarithm:
@math#ln(@math#E) @// 1.0Base-2 logarithm:
@math#log2(1024) @// 10.0Base-10 logarithm:
@math#log10(1000) @// 3.0Logarithm with arbitrary base:
@math#log(8, 2) @// 3.0Trigonometric Functions
Section titled “Trigonometric Functions”Sine (radians):
@math#sin(0) @// 0.0@math#sin(@math#PI / 2) @// 1.0Cosine (radians):
@math#cos(0) @// 1.0@math#cos(@math#PI) @// -1.0Tangent (radians):
@math#tan(0) @// 0.0Arc sine:
@math#asin(1) @// 1.57079632679490 (PI/2)Arc cosine:
@math#acos(1) @// 0.0Arc tangent:
@math#atan(1) @// 0.78539816339745 (PI/4)Two-argument arc tangent:
@math#atan2(1, 1) @// 0.78539816339745 (PI/4)Hyperbolic Functions
Section titled “Hyperbolic Functions”Hyperbolic sine:
@math#sinh(0) @// 0.0Hyperbolic cosine:
@math#cosh(0) @// 1.0Hyperbolic tangent:
@math#tanh(0) @// 0.0Angle Conversion
Section titled “Angle Conversion”to_radians
Section titled “to_radians”Degrees to radians:
@math#to_radians(180) @// 3.14159265358979to_degrees
Section titled “to_degrees”Radians to degrees:
@math#to_degrees(@math#PI) @// 180.0Rounding Functions
Section titled “Rounding Functions”Round down:
@math#floor(3.7) @// 3.0@math#floor(-3.2) @// -4.0Round up:
@math#ceil(3.2) @// 4.0@math#ceil(-3.7) @// -3.0Round to nearest:
@math#round(3.5) @// 4.0@math#round(3.4) @// 3.0truncate
Section titled “truncate”Remove fractional part:
@math#truncate(3.7) @// 3.0@math#truncate(-3.7) @// -3.0Fractional part:
@math#fract(3.7) @// 0.7@math#fract(-3.7) @// -0.7Comparison
Section titled “Comparison”is_nan
Section titled “is_nan”Check for NaN:
@math#is_nan(@math#sqrt(-1)) @// trueis_infinite
Section titled “is_infinite”Check for infinity:
@math#is_infinite(1.0 / 0.0) @// trueis_finite
Section titled “is_finite”Check for finite number:
@math#is_finite(42.0) @// trueInterpolation
Section titled “Interpolation”Linear interpolation:
@math#lerp(0, 100, 0.5) @// 50.0@math#lerp(0, 100, 0.0) @// 0.0@math#lerp(0, 100, 1.0) @// 100.0inverse_lerp
Section titled “inverse_lerp”Inverse linear interpolation:
@math#inverse_lerp(0, 100, 50) @// 0.5smoothstep
Section titled “smoothstep”Smooth Hermite interpolation:
@math#smoothstep(0, 100, 50) @// 0.5Random Numbers
Section titled “Random Numbers”random
Section titled “random”Random float between 0 and 1:
@math#random() @// e.g., 0.753213random_range
Section titled “random_range”Random number in range:
@math#random_range(1, 100) @// Random Int between 1 and 100@math#random_range(0.0, 1.0) @// Random Floatrandom_int
Section titled “random_int”Random integer:
@math#random_int(1, 6) @// Dice rollrandom_float
Section titled “random_float”Random float:
@math#random_float(0.0, 1.0)random_bool
Section titled “random_bool”Random boolean:
@math#random_bool() @// true or falserandom_pick
Section titled “random_pick”Pick random element from array:
@math#random_pick([1, 2, 3, 4, 5])shuffle
Section titled “shuffle”Shuffle array randomly:
@shuffled = @math#shuffle([1, 2, 3, 4, 5])set_seed
Section titled “set_seed”Set random seed for reproducibility:
@math#set_seed(42)Statistics
Section titled “Statistics”Arithmetic mean:
@math#mean([1, 2, 3, 4, 5]) @// 3.0median
Section titled “median”Median value:
@math#median([1, 2, 3, 4, 5]) @// 3.0Most frequent value:
@math#mode([1, 2, 2, 3, 3, 3]) @// 3variance
Section titled “variance”Population variance:
@math#variance([1, 2, 3, 4, 5]) @// 2.0std_dev
Section titled “std_dev”Standard deviation:
@math#std_dev([1, 2, 3, 4, 5]) @// 1.41421356237310Number Theory
Section titled “Number Theory”Greatest common divisor:
@math#gcd(12, 8) @// 4Least common multiple:
@math#lcm(12, 8) @// 24factorial
Section titled “factorial”Factorial:
@math#factorial(5) @// 120fibonacci
Section titled “fibonacci”Fibonacci number:
@math#fibonacci(10) @// 55is_prime
Section titled “is_prime”Prime check:
@math#is_prime(17) @// true@math#is_prime(15) @// falsenext_prime
Section titled “next_prime”Next prime number:
@math#next_prime(14) @// 17