1. Basic Library
We use a basic library under a variety of topics throughout the tutorial. The following table provides links to related pages, and lists the functions of each part of this tutorial Lua covered.
Numbering |
Library / methods |
effect |
1 |
Error Handling |
Including error handling functions, such as assert an error, such as Lua error handling the medium. |
2 |
Memory Management |
Associated with garbage collection includes automatic memory management functions, such as Lua garbage collection the medium. |
3 |
|
It opens the file and execute the contents of the file in chunks. |
4 |
|
So is the preservation of the global environment global variables (ie |
5 |
|
Function returns the current environment of use. |
6 |
|
If |
7 |
|
This function gets the value of the index and table. |
8 |
|
Using the function |
9 |
|
And |
10 |
|
And |
11 |
|
It allows the program to traverse all fields of the table. |
12 |
|
Suspend cooperative programs that are running. |
13 |
|
Print the given parameter values. |
14 |
|
Check |
15 |
|
Get |
16 |
|
The |
17 |
|
If |
18 |
|
Set the environment using the given function. |
19 |
|
Set to the element table given table. |
20 |
|
Try to convert parameter is a number. |
21 |
|
Receive any type of reasonable parameters and convert it to a string format. |
22 |
|
Returns a unique parameter type, encoded as a string. |
23 |
|
Returned to the elements in a given table. |
24 |
|
Interpreter version contains the current global string variable (not a function). |
25 |
Coroutine |
Comprising Lua coroutine explained coroutine operation function. |
2. Lua math library
Numbering |
Library or method |
description |
1 |
|
Returns the |
2 |
|
Returns the |
3 |
|
Returns the |
4 |
|
Returns the |
5 |
|
Returns the |
6 |
|
Greater than or equal returns |
7 |
|
Returns |
8 |
|
Returns |
9 |
|
Return an angle in degrees |
10 |
|
The return value |
11 |
|
Back less than or equal to |
12 |
|
Return |
13 |
|
Return |
14 |
|
|
15 |
|
Return |
16 |
|
返回 |
17 |
|
返回 |
18 |
|
返回参数中的最大值。 |
19 |
|
返回参数中的最小值。 |
20 |
|
返回两个数字, |
21 |
|
|
22 |
|
返回 |
23 |
|
以弧度为单位返回角度 |
24 |
|
此函数是ANSI C提供的简单伪随机生成器函数rand的接口。 |
25 |
|
将 |
26 |
|
返回 |
27 |
|
返回 |
28 |
|
返回 |
29 |
|
返回 |
30 |
|
返回 |
三角函数
使用三角函数的简单示例如下所示-
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.0274155677808040.01.0
0.0
1.0
180
其他常见的数学函数
使用常见数学函数的简单示例如下所示-
-- 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")
当运行上面的程序时,将得到以下输出 -
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
3. Lua操作系统工具
编号 |
库或方法 |
描述 |
1 |
|
返回程序使用的CPU时间(以秒为单位)的近似值。 |
2 |
|
返回包含日期和时间的字符串或表,根据给定的字符串格式进行格式化。 |
3 |
|
返回从时间 |
4 |
|
此功能相当于ANSI C功能系统。 它传递要由操作系统shell执行的命令。 如果命令成功终止,则第一个结果为 |
5 |
|
调用ANSI C函数出口以终止宿主程序。 如果 |
6 |
|
返回进程环境变量 |
7 |
|
使用给定名称删除文件(或POSIX系统上的空目录)。 如果此函数失败,则返回 |
8 |
|
将名为 |
9 |
|
设置程序的当前区域设置。 |
10 |
|
返回不带参数调用的当前时间,或表示给定表指定的日期和时间的时间。 此表必须包含字段年,月和日,并且可能包含字段小时(默认值为 |
11 |
|
返回一个文件名,该文件名可用于临时文件。 文件必须在使用前显式打开,并在不再需要时显式删除。 |
常见的OS功能
使用常见数学函数的简单示例如下所示 -
-- Date with format
io
.write("The date is ",os
.date("%m/%d/%Y"),"\n")
-- Date and time
io
.write("The date and time is ",os
.date(),"\n")
-- Time
io
.write("The OS time is ",os
.time(),"\n")
-- Wait for some timefori
=1,1000000doend
-- Time since Lua started
io
.write("Lua started before ",os
.clock(),"\n")
When running the above procedure, the resulting output like the following -
The date is 01/25/2018
The date and time is 01/25/18 07:38:40
The OS time is 1490615720
Lua started before 0.013
The above are just some examples of common example, you can use OS library according to their needs, it is recommended to try to use all the functions in order to become more familiar with. As remove
a function of this helps to delete files, execute help to execute OS commands.
--------------------------------------------------
Reproduced in: https: //www.yiibai.com/lua/lua_operating_system_facilities.html