Skip to content

math — математика ​

mzs
include math

INFO

Доступен в скриптах ботов.

ФункцияЧто делает
math.pi, math.eконстанты π и e
math.inf, math.nan+∞ и NaN
math.sqrt(x), math.cbrt(x)квадратный и кубический корень
math.pow(x, y)x в степени y
math.exp(x)eˣ
math.log(x), math.log2(x), math.log10(x)натуральный, двоичный, десятичный логарифм
math.sin(x), math.cos(x), math.tan(x)тригонометрия, радианы
math.asin(x), math.acos(x), math.atan(x)обратные, результат в радианах
math.atan2(y, x)угол точки (x, y)
math.sinh(x), math.cosh(x), math.tanh(x)гиперболические
math.hypot(x, y)√(x² + y²)

Все функции возвращают float. Округление, abs, min/max, clamp — методы чисел, не модуля.

Корни, степени, логарифмы ​

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]

Углы ​

Градусов нет — переводите через 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  # → true

Расстояние по ломаной ​

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

Функция как значение ​

math.sqrt без скобок — вызов без аргументов. Саму функцию берут индексом:

mzs
include math
[1, 4, 9].map(math["sqrt"])  # → [1,2,3]

inf и 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  # → true

Вне области определения — NaN или ±∞, не ошибка. Нечисловой аргумент — ошибка type.

mzs
include math
math.sqrt("x")
text
math.sqrt expects a number, got string