数学库
简介
数学库(@math)提供了一系列数学运算函数,包括基本运算、三角函数、随机数生成等。
导入方式
函数参考
基本数学函数
math.abs(x)
计算绝对值。
参数:
- x - 数字
返回值:
- 数字的绝对值
示例:
| show(`abs(-123) = `, math.abs(-123)); // 输出: 123
|
math.sqrt(x)
计算平方根。
参数:
- x - 非负数
返回值:
- 平方根值
示例:
| show(`sqrt(16) = `, math.sqrt(16)); // 输出: 4
|
math.pow(x, y)
计算幂运算。
参数:
- x - 底数
- y - 指数
返回值:
- x 的 y 次幂
示例:
| show(`pow(2, 3) = `, math.pow(2, 3)); // 输出: 8
|
三角函数
math.sin(x)
计算正弦值(角度)。
参数:
- x - 角度值
返回值:
- 正弦值
示例:
| show(`sin(90) = `, math.sin(90)); // 输出: 1
|
math.cos(x)
计算余弦值(角度)。
参数:
- x - 角度值
返回值:
- 余弦值
示例:
| show(`cos(0) = `, math.cos(0)); // 输出: 1
|
math.tan(x)
计算正切值(角度)。
参数:
- x - 角度值
返回值:
- 正切值
示例:
| show(`tan(45) = `, math.tan(45)); // 输出: 1
|
数学常数
math.pi()
返回圆周率 π 的值。
参数: 无
返回值:
- π 的值(约 3.14159...)
示例:
| show(`π = `, math.pi()); // 输出: 3.141592653589793
|
math.e()
返回自然对数底数 e 的值。
参数: 无
返回值:
- e 的值(约 2.71828...)
示例:
| show(`e = `, math.e()); // 输出: 2.718281828459045
|
最值函数
math.max(...args)
计算多个数的最大值。
参数:
- ...args - 多个数字
返回值:
- 最大值
示例:
| show(`max(10, 5, 20) = `, math.max(10, 5, 20)); // 输出: 20
|
math.min(...args)
计算多个数的最小值。
参数:
- ...args - 多个数字
返回值:
- 最小值
示例:
| show(`min(10, 5, 20) = `, math.min(10, 5, 20)); // 输出: 5
|
随机数函数
math.random()
生成 0 到 1 之间的随机数。
参数: 无
返回值:
- 0 到 1 之间的随机浮点数
示例:
| show(`random() = `, math.random()); // 输出: 0.123456...
|
math.randint(a, b)
生成指定范围内的随机整数。
参数:
- a - 最小值
- b - 最大值
返回值:
- a 到 b 之间的随机整数(包含 a 和 b)
示例:
| show(`randint(1, 100) = `, math.randint(1, 100)); // 输出: 42
|
示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | @math;
!! 基本运算
show(`abs(-123) = `, math.abs(-123));
show(`sqrt(16) = `, math.sqrt(16));
show(`pow(2, 3) = `, math.pow(2, 3));
!! 三角函数
show(`sin(90) = `, math.sin(90));
show(`cos(0) = `, math.cos(0));
show(`tan(45) = `, math.tan(45));
!! 数学常数
show(`π = `, math.pi());
show(`e = `, math.e());
!! 最值
show(`max(10, 5, 20) = `, math.max(10, 5, 20));
show(`min(10, 5, 20) = `, math.min(10, 5, 20));
!! 随机数
show(`random() = `, math.random());
show(`randint(1, 100) = `, math.randint(1, 100));
|