Math Module Documentation
Constants
Constant Name | Value | Description |
---|---|---|
E |
2.718281828459045 | Base of natural logarithm |
PI |
3.141592653589793 | Pi (π) |
TAU |
6.283185307179586 | 2π (τ) |
PHI |
1.618033988749895 | Golden ratio (φ) |
let e = math.E |
Basic Math Functions
max
math.max(values...) -> number |
- Function: Get the maximum value from a set of numbers or an array
- Parameters:
values...
: Variable parameters, can be multiple numbers or an array
- Returns: Maximum value
math.max(1, 5, 3) // Output: 5 |
min
math.min(values...) -> number |
- Function: Get the minimum value from a set of numbers or an array
- Parameters:
values...
: Variable parameters, can be multiple numbers or an array
- Returns: Minimum value
math.min(1, 5, 3) // Output: 1 |
abs
math.abs(x) -> number |
- Function: Get the absolute value of a number
- Parameters:
x
: Number
- Returns: Absolute value
math.abs(-3.5) // Output: 3.5 |
clamp
math.clamp(min, max, x) -> number |
- Function: Restrict a number within the range [min, max]
- Parameters:
min
: Minimum valuemax
: Maximum valuex
: The number to restrict
- Returns: Restricted value
math.clamp( 0, 10, 15) // Output: 10 |
sum
math.sum(values...) -> number |
- Function: Calculate the sum of numbers or an array
- Parameters:
values...
: Variable parameters, can be multiple numbers or an array
- Returns: Total sum
math.sum(1, 2, 3) // Output: 6 |
average
math.average(values...) -> number |
- Function: Calculate the average of numbers or an array
- Parameters:
values...
: Variable parameters, can be multiple numbers or an array
- Returns: Average value
math.average(1, 3, 5) // Output: 3 |
Binary Operations
bit_and
math.bit_and(a, b) -> integer |
- Function: Bitwise AND operation
- Parameters:
a
: First integerb
: Second integer
- Returns: Bitwise AND result
math.bit_and(5, 3) // Output: 1 (0101 & 0011 = 0001) |
bit_or
math.bit_or(a, b) -> integer |
- Function: Bitwise OR operation
- Parameters:
a
: First integerb
: Second integer
- Returns: Bitwise OR result
math.bit_or(5, 3) // Output: 7 (0101 | 0011 = 0111) |
bit_xor
math.bit_xor(a, b) -> integer |
- Function: Bitwise XOR operation
- Parameters:
a
: First integerb
: Second integer
- Returns: Bitwise XOR result
math.bit_xor(5, 3) // Output: 6 (0101 ^ 0011 = 0110) |
bit_not
math.bit_not(a) -> integer |
- Function: Bitwise NOT operation
- Parameters:
a
: Integer
- Returns: Bitwise NOT result
math.bit_not(5) // Output: -6 (32-bit calculation: ~0101 = 1111...1010) |
bit_shl
math.bit_shl(a, b) -> integer |
- Function: Bitwise left shift operation
- Parameters:
bit
: Number of positions to shiftx
: Integer to shift
- Returns: Left shift result
math.bit_shl(1,5) // Output: 10 (0101 << 1 = 1010) |
bit_shr
math.bit_shr(a, b) -> integer |
- Function: Bitwise right shift operation
- Parameters:
bit
: Number of positions to shiftx
: Integer to shift
- Returns: Right shift result
math.bit_shr(1, 5) // Output: 2 (0101 >> 1 = 0010) |
Trigonometric Functions
sin
math.sin(x) -> number |
- Function: Calculate the sine value (in radians)
- Parameters:
x
: Radian value
- Returns: Sine value
math.sin(math.PI/2) // Output: 1.0 |
cos
math.cos(x) -> number |
- Function: Calculate the cosine value (in radians)
- Parameters:
x
: Radian value
- Returns: Cosine value
math.cos(math.PI) // Output: -1.0 |
tan
math.tan(x) -> number |
- Function: Calculate the tangent value (in radians)
- Parameters:
x
: Radian value
- Returns: Tangent value
math.tan(math.PI/4) // Output: 0.9999999999999999 |
asin
math.asin(x) -> number |
- Function: Calculate the arcsine value
- Parameters:
x
: Value in the range [-1, 1]
- Returns: Radian value
math.asin(1) // Output: 1.5707963267948966 (π/2) |
acos
math.acos(x) -> number |
- Function: Calculate the arccosine value
- Parameters:
x
: Value in the range [-1, 1]
- Returns: Radian value
math.acos(0) // Output: 1.5707963267948966 (π/2) |
atan
math.atan(x) -> number |
- Function: Calculate the arctangent value
- Parameters:
x
: Any value
- Returns: Radian value
math.atan(1) // Output: 0.7853981633974483 (π/4) |
Hyperbolic Functions
sinh
math.sinh(x) -> number |
- Function: Calculate the hyperbolic sine value
- Parameters:
x
: Any value
- Returns: Hyperbolic sine value
math.sinh(1) // Output: 1.1752011936438014 |
cosh
math.cosh(x) -> number |
- Function: Calculate the hyperbolic cosine value
- Parameters:
x
: Any value
- Returns: Hyperbolic cosine value
math.cosh(1) // Output: 1.543080634815244 |
tanh
math.tanh(x) -> number |
- Function: Calculate the hyperbolic tangent value
- Parameters:
x
: Any value
- Returns: Hyperbolic tangent value
math.tanh(1) // Output: 0.7615941559557649 |
asinh
math.asinh(x) -> number |
- Function: Calculate the inverse hyperbolic sine value
- Parameters:
x
: Any value
- Returns: Inverse hyperbolic sine value
math.asinh(1) // Output: 0.881373587019543 |
acosh
math.acosh(x) -> number |
- Function: Calculate the inverse hyperbolic cosine value
- Parameters:
x
: x ≥ 1
- Returns: Inverse hyperbolic cosine value
math.acosh(1) // Output: 0.0 |
atanh
math.atanh(x) -> number |
- Function: Calculate the inverse hyperbolic tangent value
- Parameters:
x
: -1 < x < 1
- Returns: Inverse hyperbolic tangent value
math.atanh(0.5) // Output: 0.5493061443340548 |
π-Scaled Trigonometric Functions
sinpi
math.sinpi(x) -> number |
- Function: Calculate sin(πx)
- Parameters:
x
: Any value
- Returns: Value of sin(πx)
math.sinpi(0.5) // Output: 1.0 |
cospi
math.cospi(x) -> number |
- Function: Calculate cos(πx)
- Parameters:
x
: Any value
- Returns: Value of cos(πx)
math.cospi(1) // Output: -1.0 |
tanpi
math.tanpi(x) -> number |
- Function: Calculate tan(πx)
- Parameters:
x
: Any value
- Returns: Value of tan(πx)
math.tanpi(0.25) // Output: 1.0 |
Exponential and Logarithmic Functions
pow
math.pow(exponent,base) -> number |
- Function: Calculate base raised to the exponent
- Parameters:
exponent
: Exponentbase
: Base number
- Returns: Power value
math.pow(2, 3) // Output: 9.0 |
exp
math.exp(x) -> number |
- Function: Calculate e raised to the power of x
- Parameters:
x
: Exponent
- Returns: e^x
math.exp(1) // Output: 2.718281828459045 |
exp2
math.exp2(x) -> number |
- Function: Calculate 2 raised to the power of x
- Parameters:
x
: Exponent
- Returns: 2^x
math.exp2(3) // Output: 8.0 |
sqrt
math.sqrt(x) -> number |
- Function: Calculate the square root
- Parameters:
x
: Non-negative number
- Returns: Square root
math.sqrt(4) // Output: 2.0 |
cbrt
math.cbrt(x) -> number |
- Function: Calculate the cube root
- Parameters:
x
: Any value
- Returns: Cube root
math.cbrt(8) // Output: 2.0 |
log
math.log(x, base) -> number |
- Function: Calculate logarithm
- Parameters:
x
: Positive numberbase
: Positive number not equal to 1
- Returns: Logarithm of x to the base
math.log(8, 2) // Output: 3.0 |
log2
math.log2(x) -> number |
- Function: Calculate logarithm base 2
- Parameters:
x
: Positive number
- Returns: log₂x
math.log2(8) // Output: 3.0 |
log10
math.log10(x) -> number |
- Function: Calculate logarithm base 10
- Parameters:
x
: Positive number
- Returns: log₁₀x
math.log10(100) // Output: 2.0 |
ln
math.ln(x) -> number |
- Function: Calculate natural logarithm (base e)
- Parameters:
x
: Positive number
- Returns: ln(x)
math.ln(math.E) // Output: 1.0 |
Rounding Functions
floor
math.floor(x) -> number |
- Function: Round down to the nearest integer
- Parameters:
x
: Any value
- Returns: Largest integer not greater than x
math.floor(3.7) // Output: 3.0 |
ceil
math.ceil(x) -> number |
- Function: Round up to the nearest integer
- Parameters:
x
: Any value
- Returns: Smallest integer not less than x
math.ceil(3.2) // Output: 4.0 |
round
math.round(x) -> number |
- Function: Round to the nearest integer
- Parameters:
x
: Any value
- Returns: Nearest integer
math.round(3.2) // Output: 3.0 |
trunc
math.trunc(x) -> number |
- Function: Truncate the decimal part
- Parameters:
x
: Any value
- Returns: Integer part
math.trunc(3.7) // Output: 3.0 |
Other Functions
isodd
math.isodd(x) -> boolean |
- Function: Check if the number is odd
- Parameters:
x
: Integer
- Returns: Returns true if odd, otherwise false
math.isodd(3) // Output: true |