Lua 数学函数库

在科学计算与工程计算领域,我们都需要用到大量的数学函数。在 Lua 的数学库提供了大量的数学函数,如下表所示:

S.N. 函数与功能
1 math.abs(x):返回 x 的绝对值。
2 math.acos(x):返回 x 的反余弦值(弧度)。
3 math.asin(x):返回 x 的反正弦值(弧度)。
4 math.atan(x):返回 x 的反正切值(弧度)。
5 math.atan2(y,x),返回 y/x 的反正切值,使用两个参数的符号查找象限( x 为 0 时也能正确的处理)。
6 math.ceil(x):返回大于或等于 x 的最小整数。
7 math.cos(x):返回 x 的余弦值(x 以弧度为单位)。
8 math.cosh(x):返回 x 的双曲余弦值。
9 math.deg(x):返回 x 的角度值(x为弧度)。
10 math.exp(x):返回 e 的 x 次幂。
11 math.floor(x):返回小于或等于 x 的最大整数。
12 math.fmod(x,y): 返回 x%y。
13 math.frexp(x):返回两个值 m,e,满足 x = m*2^e。其中,e 是整数,m 的绝对值属于区间 [0.5,1)。
14 math.huge:最大值,不小于任何其它数值。
15 math.ldexp(m,e):返回 m*2^e(e 为整数)。
16 math.log(x):计算自然对数。
17 math.log10(x):计数以 10 为底的对数。
18 math.max(x,...):返回输入参数的最大值。
19 math.min(x,...):返回输入参数的最小值。
20 math.modf(x):返回 x 的整数部分与小数部分。
21 math.pi:数值 PI。
22 math.pow(x,y):等价于 x^y。
23 math.rad(x):返回角度 x 的弧度值。
24 math.random([m,[n]]):该函数直接调用 ANSI C 的伪随机生成函数。无参数时,生成 [0,1) 区间的均匀分布的随机值;只传入参数 m 时,函数生成一个位于区间 [1,m] 的均匀分布伪随机值;同时传入参数 m,n 时,生成位于区间 [m,n] 的均匀分布伪随机值。
25 math.randomseed(x):初始化伪随机数生成器种子值。
26 math.sin(x):返回 x 的正弦值。
27 math.sinh(x):返回 x 的双曲正弦值。
28 math.sqrt(x):返回 x 的平方根。
29 math.tan(x):返回 x 的正切值。
30 math.tanh(x):返回 x 的双曲正切值。

三角函数

三角函数的使用方法示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
radianVal = math.rad(math.pi / 2)

io.write(radianVal,"\n")

-- Sin value of 90(math.pi / 2) degrees
io.write(string.format("%.1f ", math.sin(radianVal)),"\n")
-- Cos value of 90(math.pi / 2) degrees
io.write(string.format("%.1f ", math.cos(radianVal)),"\n")
-- Tan value of 90(math.pi / 2) degrees
io.write(string.format("%.1f ", math.tan(radianVal)),"\n")
-- Cosh value of 90(math.pi / 2) degrees
io.write(string.format("%.1f ", math.cosh(radianVal)),"\n")
-- Pi Value in degrees
io.write(math.deg(math.pi),"\n")
```

运行上面的程序,我们可以得到如下的输出结果:

0.027415567780804
0.0
1.0
0.0
1.0
180

1
2
3

## 另外一些常用的数学函数

– Floor
io.write(“Floor of 10.5055 is “, math.floor(10.5055),”\n”)
– Ceil
io.write(“Ceil of 10.5055 is “, math.ceil(10.5055),”\n”)
– Square root
io.write(“Square root of 16 is “,math.sqrt(16),”\n”)
– Power
io.write(“10 power 2 is “,math.pow(10,2),”\n”)
io.write(“100 power 0.5 is “,math.pow(100,0.5),”\n”)
– Absolute
io.write(“Absolute value of -10 is “,math.abs(-10),”\n”)
–Random
math.randomseed(os.time())
io.write(“Random number between 1 and 100 is “,math.random(),”\n”)
–Random between 1 to 100
io.write(“Random number between 1 and 100 is “,math.random(1,100),”\n”)
–Max
io.write(“Maximum in the input array is “,math.max(1,100,101,99,999),”\n”)
–Min
io.write(“Minimum in the input array is “,math.min(1,100,101,99,999),”\n”)

1
2
3

运行上面的程序,我们可以得到如下的输出结果:

Floor of 10.5055 is 10
Ceil of 10.5055 is 11
Square root of 16 is 4
10 power 2 is 100
100 power 0.5 is 10
Absolute value of -10 is 10
Random number between 1 and 100 is 0.22876674703207
Random number between 1 and 100 is 7
Maximum in the input array is 999
Minimum in the input array is 1


上面的例子只是给出了数学函数一些简单的使用方法,在实际中我们可以根据自己的需要进行选择和使用。通过更多的练习可以对这些函数更加的熟悉。