Skip to content
Draft

math

The math module provides mathematical functions and constants.

import math
import { sqrt, sin, cos, pi } from math
@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 value

Absolute value:

@math#abs(-5) @// 5
@math#abs(-3.14) @// 3.14

Minimum of two values:

@math#min(10, 20) @// 10
@math#min(3.14, 2.71) @// 2.71

Maximum of two values:

@math#max(10, 20) @// 20

Clamp value between min and max:

@math#clamp(150, 0, 100) @// 100
@math#clamp(-10, 0, 100) @// 0
@math#clamp(50, 0, 100) @// 50

Sign of a number:

@math#sign(-5) @// -1
@math#sign(0) @// 0
@math#sign(5) @// 1

Square root:

@math#sqrt(144) @// 12.0
@math#sqrt(2) @// 1.41421356237310

Cube root:

@math#cbrt(27) @// 3.0

Power function:

@math#pow(2, 10) @// 1024.0
@math#pow(27, 1/3) @// 3.0

Exponential function (e^x):

@math#exp(1) @// 2.71828182845905
@math#exp(0) @// 1.0

Power of 10:

@math#pow10(3) @// 1000.0

Natural logarithm:

@math#ln(@math#E) @// 1.0

Base-2 logarithm:

@math#log2(1024) @// 10.0

Base-10 logarithm:

@math#log10(1000) @// 3.0

Logarithm with arbitrary base:

@math#log(8, 2) @// 3.0

Sine (radians):

@math#sin(0) @// 0.0
@math#sin(@math#PI / 2) @// 1.0

Cosine (radians):

@math#cos(0) @// 1.0
@math#cos(@math#PI) @// -1.0

Tangent (radians):

@math#tan(0) @// 0.0

Arc sine:

@math#asin(1) @// 1.57079632679490 (PI/2)

Arc cosine:

@math#acos(1) @// 0.0

Arc tangent:

@math#atan(1) @// 0.78539816339745 (PI/4)

Two-argument arc tangent:

@math#atan2(1, 1) @// 0.78539816339745 (PI/4)

Hyperbolic sine:

@math#sinh(0) @// 0.0

Hyperbolic cosine:

@math#cosh(0) @// 1.0

Hyperbolic tangent:

@math#tanh(0) @// 0.0

Degrees to radians:

@math#to_radians(180) @// 3.14159265358979

Radians to degrees:

@math#to_degrees(@math#PI) @// 180.0

Round down:

@math#floor(3.7) @// 3.0
@math#floor(-3.2) @// -4.0

Round up:

@math#ceil(3.2) @// 4.0
@math#ceil(-3.7) @// -3.0

Round to nearest:

@math#round(3.5) @// 4.0
@math#round(3.4) @// 3.0

Remove fractional part:

@math#truncate(3.7) @// 3.0
@math#truncate(-3.7) @// -3.0

Fractional part:

@math#fract(3.7) @// 0.7
@math#fract(-3.7) @// -0.7

Check for NaN:

@math#is_nan(@math#sqrt(-1)) @// true

Check for infinity:

@math#is_infinite(1.0 / 0.0) @// true

Check for finite number:

@math#is_finite(42.0) @// true

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.0

Inverse linear interpolation:

@math#inverse_lerp(0, 100, 50) @// 0.5

Smooth Hermite interpolation:

@math#smoothstep(0, 100, 50) @// 0.5

Random float between 0 and 1:

@math#random() @// e.g., 0.753213

Random number in range:

@math#random_range(1, 100) @// Random Int between 1 and 100
@math#random_range(0.0, 1.0) @// Random Float

Random integer:

@math#random_int(1, 6) @// Dice roll

Random float:

@math#random_float(0.0, 1.0)

Random boolean:

@math#random_bool() @// true or false

Pick random element from array:

@math#random_pick([1, 2, 3, 4, 5])

Shuffle array randomly:

@shuffled = @math#shuffle([1, 2, 3, 4, 5])

Set random seed for reproducibility:

@math#set_seed(42)

Arithmetic mean:

@math#mean([1, 2, 3, 4, 5]) @// 3.0

Median value:

@math#median([1, 2, 3, 4, 5]) @// 3.0

Most frequent value:

@math#mode([1, 2, 2, 3, 3, 3]) @// 3

Population variance:

@math#variance([1, 2, 3, 4, 5]) @// 2.0

Standard deviation:

@math#std_dev([1, 2, 3, 4, 5]) @// 1.41421356237310

Greatest common divisor:

@math#gcd(12, 8) @// 4

Least common multiple:

@math#lcm(12, 8) @// 24

Factorial:

@math#factorial(5) @// 120

Fibonacci number:

@math#fibonacci(10) @// 55

Prime check:

@math#is_prime(17) @// true
@math#is_prime(15) @// false

Next prime number:

@math#next_prime(14) @// 17