Math 模块文档
常量 (Constants)
常量名 | 值 | 描述 |
---|---|---|
E |
2.718281828459045 | 自然对数的底数 |
PI |
3.141592653589793 | 圆周率π |
TAU |
6.283185307179586 | 2π (τ) |
PHI |
1.618033988749895 | 黄金比例 (φ) |
let e = math.E
let pi = math.PI
"E = ", e, ", PI = ", pi
基础数学函数 (Basic math functions)
max
math.max(values...) -> number
功能: 获取一组数值或数组中的最大值
参数:
values...
: 可变参数,可以是多个数值或一个数组
返回: 最大值
math.max(1, 5, 3) # 输出: 5
math.max([2, 8, 4]) # 输出: 8
min
math.min(values...) -> number
功能: 获取一组数值或数组中的最小值
参数:
values...
: 可变参数,可以是多个数值或一个数组
返回: 最小值
math.min(1, 5, 3) # 输出: 1
math.min([2, 8, 4]) # 输出: 2
abs
math.abs(x) -> number
功能: 获取数值的绝对值
参数:
x
: 数值
返回: 绝对值
math.abs(-3.5) # 输出: 3.5
clamp
math.clamp( min, max, x) -> number
功能: 将数值限制在[min, max]范围内
参数:
min
: 最小值max
: 最大值x
: 要限制的数值
返回: 限制后的值
math.clamp(0, 10, 15) # 输出: 10
math.clamp(0, 10, -5) # 输出: 0
sum
math.sum(values...) -> number
功能: 计算数值或数组的和
参数:
values...
: 可变参数,可以是多个数值或一个数组
返回: 总和
math.sum(1, 2, 3) # 输出: 6
math.sum([4, 5, 6]) # 输出: 15
average
math.average(values...) -> number
功能: 计算数值或数组的平均值
参数:
values...
: 可变参数,可以是多个数值或一个数组
返回: 平均值
math.average(1, 3, 5) # 输出: 3
math.average([2, 4, 6]) # 输出: 4
位运算 (Binary operations)
bit_and
math.bit_and(a, b) -> integer
功能: 按位与运算
参数:
a
: 第一个整数b
: 第二个整数
返回: 按位与结果
math.bit_and(5, 3) # 输出: 1 (0101 & 0011 = 0001)
bit_or
math.bit_or(a, b) -> integer
功能: 按位或运算
参数:
a
: 第一个整数b
: 第二个整数
返回: 按位或结果
math.bit_or(5, 3) # 输出: 7 (0101 | 0011 = 0111)
bit_xor
math.bit_xor(a, b) -> integer
功能: 按位异或运算
参数:
a
: 第一个整数b
: 第二个整数
返回: 按位异或结果
math.bit_xor(5, 3) # 输出: 6 (0101 ^ 0011 = 0110)
bit_not
math.bit_not(a) -> integer
功能: 按位取反运算
参数:
a
: 整数
返回: 按位取反结果
math.bit_not(5) # 输出: -6 (按32位计算: ~0101 = 1111...1010)
bit_shl
math.bit_shl(a, b) -> integer
功能: 按位左移运算
参数:
bit
: 移动位数x
: 要移动的整数
返回: 左移结果
math.bit_shl(1, 5) # 输出: 10 (0101 << 1 = 1010)
bit_shr
math.bit_shr(a, b) -> integer
功能: 按位右移运算
参数:
bit
: 移动位数x
: 要移动的整数
返回: 右移结果
math.bit_shr(1, 5)) # 输出: 2 (0101 >> 1 = 0010)
三角函数 (Trigonometric functions)
sin
math.sin(x) -> number
功能: 计算正弦值(弧度)
参数:
x
: 弧度值
返回: 正弦值
math.sin(math.PI/2) # 输出: 1.0
cos
math.cos(x) -> number
功能: 计算余弦值(弧度)
参数:
x
: 弧度值
返回: 余弦值
math.cos(math.PI) # 输出: -1.0
tan
math.tan(x) -> number
功能: 计算正切值(弧度)
参数:
x
: 弧度值
返回: 正切值
math.tan(math.PI/4) # 输出: 0.9999999999999999
asin
math.asin(x) -> number
功能: 计算反正弦值
参数:
x
: 值在[-1, 1]范围内
返回: 弧度值
math.asin(1) # 输出: 1.5707963267948966 (π/2)
acos
math.acos(x) -> number
功能: 计算反余弦值
参数:
x
: 值在[-1, 1]范围内
返回: 弧度值
math.acos(0) # 输出: 1.5707963267948966 (π/2)
atan
math.atan(x) -> number
功能: 计算反正切值
参数:
x
: 任意数值
返回: 弧度值
math.atan(1) # 输出: 0.7853981633974483 (π/4)
双曲函数 (Hyperbolic functions)
sinh
math.sinh(x) -> number
功能: 计算双曲正弦值
参数:
x
: 任意数值
返回: 双曲正弦值
math.sinh(1) # 输出: 1.1752011936438014
cosh
math.cosh(x) -> number
功能: 计算双曲余弦值
参数:
x
: 任意数值
返回: 双曲余弦值
math.cosh(1) # 输出: 1.543080634815244
tanh
math.tanh(x) -> number
功能: 计算双曲正切值
参数:
x
: 任意数值
返回: 双曲正切值
math.tanh(1) # 输出: 0.7615941559557649
asinh
math.asinh(x) -> number
功能: 计算反双曲正弦值
参数:
x
: 任意数值
返回: 反双曲正弦值
math.asinh(1) # 输出: 0.881373587019543
acosh
math.acosh(x) -> number
功能: 计算反双曲余弦值
参数:
x
: x ≥ 1
返回: 反双曲余弦值
math.acosh(1) # 输出: 0.0
atanh
math.atanh(x) -> number
功能: 计算反双曲正切值
参数:
x
: -1 < x < 1
返回: 反双曲正切值
math.atanh(0.5) # 输出: 0.5493061443340548
π倍三角函数 (π-scaled trigonometric functions)
sinpi
math.sinpi(x) -> number
功能: 计算sin(πx)
参数:
x
: 任意数值
返回: sin(πx)的值
math.sinpi(0.5) # 输出: 1.0
cospi
math.cospi(x) -> number
功能: 计算cos(πx)
参数:
x
: 任意数值
返回: cos(πx)的值
math.cospi(1) # 输出: -1.0
tanpi
math.tanpi(x) -> number
功能: 计算tan(πx)
参数:
x
: 任意数值
返回: tan(πx)的值
math.tanpi(0.25) # 输出: 1.0
指数和对数函数 (Exponential and logarithmic functions)
pow
math.pow(exponent,base) -> number
功能: 计算base的exponent次幂
参数:
exponent
: 指数base
: 底数
返回: 幂值
math.pow(2, 3) # 输出: 9.0
exp
math.exp(x) -> number
功能: 计算e的x次幂
参数:
x
: 指数
返回: e^x
math.exp(1) # 输出: 2.718281828459045
exp2
math.exp2(x) -> number
功能: 计算2的x次幂
参数:
x
: 指数
返回: 2^x
math.exp2(3) # 输出: 8.0
sqrt
math.sqrt(x) -> number
功能: 计算平方根
参数:
x
: 非负数
返回: 平方根
math.sqrt(4) # 输出: 2.0
cbrt
math.cbrt(x) -> number
功能: 计算立方根
参数:
x
: 任意数值
返回: 立方根
math.cbrt(8) # 输出: 2.0
log
math.log(x, base) -> number
功能: 计算对数
参数:
x
: 正数base
: 正数且不等于1
返回: 以base为底x的对数
math.log(8, 2) # 输出: 3.0
log2
math.log2(x) -> number
功能: 计算以2为底的对数
参数:
x
: 正数
返回: log₂x
math.log2(8) # 输出: 3.0
log10
math.log10(x) -> number
功能: 计算以10为底的对数
参数:
x
: 正数
返回: log₁₀x
math.log10(100) # 输出: 2.0
ln
math.ln(x) -> number
功能: 计算自然对数(以e为底)
参数:
x
: 正数
返回: ln(x)
math.ln(math.E) # 输出: 1.0
舍入函数 (Rounding functions)
floor
math.floor(x) -> number
功能: 向下取整
参数:
x
: 任意数值
返回: 不大于x的最大整数
math.floor(3.7) # 输出: 3.0
math.floor(-1.2) # 输出: -2.0
ceil
math.ceil(x) -> number
功能: 向上取整
参数:
x
: 任意数值
返回: 不小于x的最小整数
math.ceil(3.2) # 输出: 4.0
math.ceil(-1.7) # 输出: -1.0
round
math.round(x) -> number
功能: 四舍五入
参数:
x
: 任意数值
返回: 最接近的整数
math.round(3.2) # 输出: 3.0
math.round(3.7) # 输出: 4.0
trunc
math.trunc(x) -> number
功能: 截断小数部分
参数:
x
: 任意数值
返回: 整数部分
math.trunc(3.7) # 输出: 3.0
math.trunc(-1.7) # 输出: -1.0
其他函数 (Other functions)
isodd
math.isodd(x) -> boolean
功能: 判断是否为奇数
参数:
x
: 整数
返回: 如果是奇数返回true,否则false
math.isodd(3) # 输出: true
math.isodd(4) # 输出: false