Skip to content

API Math

Liu.Yandong.Hanks edited this page Aug 19, 2026 · 3 revisions

Math

数学常量与函数

Mathematical constants and functions

脚本 API 目录

Script API Directory

概述

Overview

Math 提供纯数值运算。三角函数以弧度为单位;random 返回伪随机数,不应用于安全令牌或密码学用途。

Math provides pure numeric operations. Trigonometric functions use radians; random returns a pseudo-random value and must not be used for security tokens or cryptography.

常量

Constants

  • Math.PI
    圆周率。

    Pi.
    return Math.PI;

  • Math.E
    自然常数。

    Euler's number.
    return Math.E;

  • Math.Tau
    2 * PI

    2 * PI.
    return Math.Tau;

  • Math.DEG_PER_RAD
    弧度转角度系数。

    Radians-to-degrees factor.
    return Math.PI * Math.DEG_PER_RAD; // 180

Math.abs(value)

Parameters:

  • value
    数值。

    Number.

Returns

绝对值。

Absolute value.

return Math.abs(-4); // 4

Math.max(...values)

Parameters:

  • values
    一个或多个数值。

    One or more numbers.

Returns

最大值。

Largest value.

return Math.max(2, 8, 5); // 8

Math.min(...values)

Parameters:

  • values
    一个或多个数值。

    One or more numbers.

Returns

最小值。

Smallest value.

return Math.min(2, 8, 5); // 2

Math.random()

Parameters:

  • 无。

    None.

Returns

伪随机数。

Pseudo-random number.

var index = Math.floor(Math.random() * 10);
return index;

Math.log(value)Math.exp(value)

Parameters:

  • value
    数值。

    Number.

Returns

自然对数或 e 的幂。

Natural logarithm or e raised to the value.

var restored = Math.exp(Math.log(10));
return Math.round(restored); // 10

Math.pow(x, y)

Parameters:

  • x
    底数。

    Base.

  • y
    指数。

    Exponent.

Returns

xy 次幂。

x raised to y.

return Math.pow(2, 3); // 8

Math.sin(value)Math.cos(value)Math.tan(value)

Parameters:

  • value
    弧度。

    Angle in radians.

Returns

对应三角函数结果。

Corresponding trigonometric result.

var sine = Math.sin(Math.PI / 2);
var cosine = Math.cos(0);
var tangent = Math.tan(0);
return sine + cosine + tangent; // 2

Math.asin(value)Math.acos(value)Math.atan(value)

Parameters:

  • value
    数值。

    Number.

Returns

弧度角。

Angle in radians.

var arcsine = Math.asin(1);
var arccosine = Math.acos(1);
var arctangent = Math.atan(1);
return arcsine + arccosine + arctangent;

Math.ceil(value)Math.floor(value)Math.round(value)

Parameters:

  • value
    数值。

    Number.

Returns

分别为向上、向下和最接近整数。

Upward, downward, and nearest-integer rounding.

var up = Math.ceil(1.2);
var down = Math.floor(1.8);
return Math.round(up + down); // 3

说明

此页每个 Math 成员均在上面的常量表或成员示例中出现。

Every Math member appears in the constants table or a member example above.

Clone this wiki locally