math — mathematics
mzs
include mathINFO
Available in bot scripts.
| Function | What it does |
|---|---|
math.pi, math.e | the constants π and e |
math.inf, math.nan | +∞ and NaN |
math.sqrt(x), math.cbrt(x) | square and cube root |
math.pow(x, y) | x to the power y |
math.exp(x) | eˣ |
math.log(x), math.log2(x), math.log10(x) | natural, binary, decimal logarithm |
math.sin(x), math.cos(x), math.tan(x) | trigonometry, radians |
math.asin(x), math.acos(x), math.atan(x) | inverses, result in radians |
math.atan2(y, x) | the angle of the point (x, y) |
math.sinh(x), math.cosh(x), math.tanh(x) | hyperbolic |
math.hypot(x, y) | √(x² + y²) |
All functions return a float. Rounding, abs, min/max, clamp are number methods, not the module's.
Roots, powers, logarithms
mzs
include math
math.sqrt(2) # → 1.4142135623730951
math.sqrt(4) # → 2.0
math.sqrt(4).int # → 2
math.pow(2, 10) # → 1024.0
math.hypot(3, 4) # → 5.0
[math.log(math.e), math.log2(1024), math.log10(1000)] # → [1,10,3]Angles
There are no degrees: convert through pi.
mzs
include math
fn rad(d) { d * math.pi / 180 }
math.sin(rad(30)) # → 0.49999999999999994
math.sin(rad(30)).round(2) # → 0.5
math.atan2(1, 1) == math.pi / 4 # → trueLength of a polyline
mzs
include math
pts = [[0, 0], [3, 4], [6, 8]]
pts.each_cons(2).map { (p) -> math.hypot(p[1][0] - p[0][0], p[1][1] - p[0][1]) }.sum # → 10.0A function as a value
math.sqrt without parentheses is a call with no arguments. To get the function itself, index it:
mzs
include math
[1, 4, 9].map(math["sqrt"]) # → [1,2,3]inf and nan
mzs
include math
[4, 9, 2].reduce(math.inf) { (lo, x) -> min(lo, x) } # → 2
math.sqrt(-1) # → NaN
math.log(0) # → -Infinity
math.nan == math.nan # → false
math.sqrt(-1).is_nan # → trueOutside the domain you get NaN or ±∞, not an error. A non-numeric argument is a type error.
mzs
include math
math.sqrt("x")text
math.sqrt expects a number, got string