Math
Math 命名空间包含在全局作用域和 Math 命名空间中都可用的函数。
// Using the Math namespacex: Math.abs(-10); // sets x to 10slint
// Using the functions via global scope. No need for 'Math' prefix.x: abs(-10); // sets x to 10slint
一些数学函数可以以后缀风格使用,这可以使代码更具可读性。
// Using the postfix style.x: (-10).abs(); // sets x to 10slint
T 类型
许多数学函数可以与任何 数值类型 一起使用,例如 angle、duration、float、int、length 和 percent。在本页中它们表示为 T。
通用数学函数
Section titled “通用数学函数”abs(T) -> T
Section titled “abs(T) -> T”返回绝对值,其中 T 是数值类型。
Math.abs(-10); // returns 10abs(-10px); // returns 10px(-10).abs(); // returns 10slint
ceil(float) -> int
Section titled “ceil(float) -> int”返回向上舍入到最接近整数的值。
Math.ceil(4); // returns 4Math.ceil(2.3); // returns 3Math.ceil(-1.5); // returns -1slint
floor(float) -> int
Section titled “floor(float) -> int”返回向下舍入到最接近整数的值。
Math.floor(4); // returns 4Math.floor(2.3); // returns 2Math.floor(-1.5); // returns -2slint
round(float) -> int
Section titled “round(float) -> int”返回舍入到最接近整数的值
Math.round(4.5); // returns 5Math.round(4.4); // returns 4Math.round(-1.2); // returns -1slint
sign(float) -> float
Section titled “sign(float) -> float”返回 1 或 -1,表示作为参数传入的数字的符号。 如果输入为 0 或 -0,则返回 1。
Math.sign(10); // returns 1Math.sign(-30); // returns -1Math.sign(0); // returns 1slint
clamp(T, T, T) -> T
Section titled “clamp(T, T, T) -> T”接受 value、minimum 和 maximum,如果 value > maximum 则返回 maximum,
如果 value < minimum 则返回 minimum,否则返回 value。
log(float, float) -> float
Section titled “log(float, float) -> float”返回以第二个值为底的第一个值的对数
ln(float) -> float
Section titled “ln(float) -> float”返回该值的自然对数。等同于 log(x, e)
min(T, T) -> T
Section titled “min(T, T) -> T”max(T, T) -> T
Section titled “max(T, T) -> T”返回具有最小(或最大)值的参数。所有参数必须是相同的数值类型。
Math.min(1, 2); // returns 1Math.min(2, 1); // returns 1Math.max(1, 2); // returns 2Math.max(2, 1); // returns 2slint
mod(T, T) -> T
Section titled “mod(T, T) -> T”执行取模运算,其中 T 是某种数值类型。 返回参数进行欧几里得除法后的余数。 这始终返回 0 与第二个值绝对值之间的正数。
sqrt(float) -> float
Section titled “sqrt(float) -> float”平方根
pow(float, float) -> float
Section titled “pow(float, float) -> float”返回第一个值的第二个值次幂
exp(float) -> float
Section titled “exp(float) -> float”返回 e 的给定值次幂
acos(float) -> angle
Section titled “acos(float) -> angle”返回数字的反余弦。反余弦是余弦为该数字的角度。
asin(float) -> angle
Section titled “asin(float) -> angle”返回数字的反正弦。反正弦是正弦为该数字的角度。
atan(float) -> angle
Section titled “atan(float) -> angle”返回数字的反正切。
atan2(float, float) -> angle
Section titled “atan2(float, float) -> angle”cos(angle) -> float
Section titled “cos(angle) -> float”sin(angle) -> float
Section titled “sin(angle) -> float”tan(angle) -> float
Section titled “tan(angle) -> float”三角函数。注意应使用 deg 或 rad 单位输入
(例如 cos(90deg) 或 sin(slider.value * 1deg))。
© 2026 SixtyFPS GmbH