Math Module
The math module provides mathematical constants and functions for numeric
computation. Import it with math. prefix.
Constants
| Constant | Value | Description |
|---|
math.pi | 3.141592653589793 | Ratio of a circle's circumference to its diameter |
math.e | 2.718281828459045 | Euler's number, base of the natural logarithm |
math.tau | 6.283185307179586 | Circle constant, equal to 2 * pi |
math.inf | +Inf | Positive infinity |
math.nan | NaN | Not a number |
math.pi // 3.141592653589793
math.tau // 6.283185307179586
math.e // 2.718281828459045
Basic Functions
| Function | Description |
|---|
math.abs(x) | Absolute value of x |
math.sign(x) | Sign of x: -1, 0, or 1 |
math.ceil(x) | Round up to the nearest integer |
math.floor(x) | Round down to the nearest integer |
math.round(x) | Round to the nearest integer |
math.trunc(x) | Truncate the decimal portion |
math.clamp(x, min, max) | Constrain x to the range [min, max] |
math.abs(-7) // 7
math.ceil(2.3) // 3.0
math.floor(2.9) // 2.0
math.round(2.5) // 3.0
math.trunc(2.9) // 2.0
math.clamp(15, 0, 10) // 10
math.sign(-42) // -1
Min, Max, and Sum
| Function | Description |
|---|
math.min(a, b) | Smaller of two values |
math.max(a, b) | Larger of two values |
math.sum(list) | Sum of all elements in a list |
math.min(3, 7) // 3
math.max(3, 7) // 7
math.sum([1, 2, 3, 4]) // 10
Powers and Logarithms
| Function | Description |
|---|
math.sqrt(x) | Square root |
math.cbrt(x) | Cube root |
math.pow(x, y) | x raised to the power y |
math.exp(x) | e raised to the power x |
math.log(x) | Natural logarithm (base e) |
math.log10(x) | Base-10 logarithm |
math.log2(x) | Base-2 logarithm |
math.sqrt(16) // 4.0
math.cbrt(27) // 3.0
math.pow(2, 10) // 1024.0
math.exp(1) // 2.718281828459045
math.log(math.e) // 1.0
math.log10(1000) // 3.0
math.log2(256) // 8.0
Trigonometry
| Function | Description |
|---|
math.sin(x) | Sine of x (radians) |
math.cos(x) | Cosine of x (radians) |
math.tan(x) | Tangent of x (radians) |
math.asin(x) | Arcsine, returns radians |
math.acos(x) | Arccosine, returns radians |
math.atan(x) | Arctangent, returns radians |
math.atan2(y, x) | Two-argument arctangent |
math.hypot(x, y) | Hypotenuse: sqrt(xx + yy) |
math.sin(math.pi / 2) // 1.0
math.cos(0) // 1.0
math.atan2(1, 1) // 0.7853981633974483
math.hypot(3, 4) // 5.0
Hyperbolic Functions
| Function | Description |
|---|
math.sinh(x) | Hyperbolic sine |
math.cosh(x) | Hyperbolic cosine |
math.tanh(x) | Hyperbolic tangent |
math.sinh(1) // 1.1752011936438014
math.cosh(0) // 1.0
math.tanh(0) // 0.0
Angle Conversion
| Function | Description |
|---|
math.degrees(radians) | Convert radians to degrees |
math.radians(degrees) | Convert degrees to radians |
math.degrees(math.pi) // 180.0
math.radians(180) // 3.141592653589793
Predicates
| Function | Description |
|---|
math.is_finite(x) | True if x is neither infinite nor NaN |
math.is_inf(x) | True if x is positive or negative infinity |
math.is_nan(x) | True if x is NaN |
math.is_finite(42) // true
math.is_finite(math.inf) // false
math.is_inf(math.inf) // true
math.is_nan(math.nan) // true
math.is_nan(0.0) // false