Lua API functions

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

dofile ([filename])

It opens the file and execute the contents of the file in chunks.

4

_G

So is the preservation of the global environment global variables (ie _G._G = _G).

5

getfenv ([f])

Function returns the current environment of use.

6

getmetatable (object)

If objectnot metatable, return nil. Otherwise, if objectthe metatablehave a __metatablefield,

7

ipairs (t)

This function gets the value of the index and table.

8

load (func [, chunkname])

Using the function functo load a block to obtain its fragments.

9

loadfile ([filename]))

And loadthe like, but if no file name, the file from filenameor standard input acquisition block.

10

loadstring (string [, chunkname])

And loadlike functions, but the given string acquisition block.

11

next (table [, index])

It allows the program to traverse all fields of the table.

12

pairs (t)

Suspend cooperative programs that are running.

13

print (...)

Print the given parameter values.

14

rawequal (v1, v2)

Check v1equals v2, without invoking any free methods. It returns a Boolean value.

15

rawget (table, index)

Get table [index]the value, without invoking any method. tableMust be the table; indexit may be any value.

16

rawset (table, index, value)

The table [index]value is set to value, without invoking any method.

17

select (index, ...)

If indexdigital, all the parameters of the parameter number index is returned.

18

setfenv (f, table)

Set the environment using the given function.

19

setmetatable (table, metatable)

Set to the element table given table.

20

tonumber (e [, base])

Try to convert parameter is a number.

21

tostring (e)

Receive any type of reasonable parameters and convert it to a string format.

22

type (v)

Returns a unique parameter type, encoded as a string.

23

unpack (list [, i [, j]])

Returned to the elements in a given table.

24

_VERSION

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

math.abs(x)

Returns the xabsolute value of.

2

math.acos(x)

Returns the xarc cosine (in radians).

3

math.asin(x)

Returns the xarc sine (in radians).

4

math.atan(x)

Returns the xarctangent (in radians).

5

math.atan2(y,x)

Returns the y / xarctangent (in radians), but use two symbols to look up the parameter quadrant results (which also correctly handle xthe case of zero.)

6

math.ceil(x)

Greater than or equal returns xthe smallest integer.

7

math.cos(x)

Returns xthe cosine (assuming radians).

8

math.cosh(x)

Returns xthe hyperbolic cosine.

9

math.deg(x)

Return an angle in degrees x(in radians).

10

math.exp(x)

The return value eof xpower.

11

math.floor(x)

Back less than or equal to xthe largest integer.

12

math.fmod(x,y)

Return xdivided by the yremainder of the quotient is rounded to zero.

13

math.frexp(x)

Return mand e, so x = m2e, eis an integer, mthe absolute value [0.5,1](or when the range of xzero to zero).

14

math.huge

HUGE_VALValue is a value greater than or equal to any other value.

15

math.ldexp(m, e)

Return m2e( ean integer).

16

math.log(x)

返回x的自然对数。

17

math.log10(x)

返回x的以10为底的对数。

18

math.max(x,...)

返回参数中的最大值。

19

math.min(x,...)

返回参数中的最小值。

20

math.modf(x)

返回两个数字,x的整数部分和x的小数部分。

21

math.pi

pi的值。

22

math.pow(x,y)

返回xy方。(也可以使用表达式x ^ y来计算此值。)

23

math.rad(x)

以弧度为单位返回角度x(以度为单位)。

24

math.random([m [, n]])

此函数是ANSI C提供的简单伪随机生成器函数rand的接口。

25

math.randomseed(x)

x设置为伪随机生成器的“种子”:相等的种子产生相等的数字序列。

26

math.sin(x)

返回x的正弦值(假设为弧度)。

27

math.sinh(x)

返回x的双曲正弦值。

28

math.sqrt(x)

返回x的平方根。(也可以使用表达式x ^ 0.5来计算此值。)

29

math.tan(x)

返回x的正切(假设为弧度)。

30

math.tanh(x)

返回x的双曲正切值。

三角函数

使用三角函数的简单示例如下所示-

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.0 1.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

os.clock()

返回程序使用的CPU时间(以秒为单位)的近似值。

2

os.date([format[, time]])

返回包含日期和时间的字符串或表,根据给定的字符串格式进行格式化。

3

os.difftime(t2,t1)

返回从时间t1到时间t2的秒数。在POSIX,Windows和其他一些系统中,恰好是t2-t1的值。

4

os.execute([command])

此功能相当于ANSI C功能系统。 它传递要由操作系统shell执行的命令。 如果命令成功终止,则第一个结果为true,否则为nil

5

os.exit([code[, close])

调用ANSI C函数出口以终止宿主程序。 如果codetrue,则返回状态为EXIT_SUCCESS; 如果codefalse,则返回状态为EXIT_FAILURE; 如果code是数字,则返回的状态是此数字。

6

os.getenv(varname)

返回进程环境变量varname的值,如果未定义变量,则返回nil

7

os.remove(filename)

使用给定名称删除文件(或POSIX系统上的空目录)。 如果此函数失败,则返回nil,以及描述错误和错误代码的字符串。

8

os.rename(oldname, newname)

将名为oldname的文件或目录重命名为newname。 如果此函数失败,则返回nil,以及描述错误和错误代码的字符串。

9

os.setlocale(locale [,category])

设置程序的当前区域设置。 locale是一个依赖于系统的字符串,用于指定语言环境; category是一个可选字符串,用于描述要更改的类别:allcollatectypecurrencynumerictime; 默认类别(category)是"all"。该函数返回新语言环境的名称,如果无法满足请求,则返回nil

10

os.time([table])

返回不带参数调用的当前时间,或表示给定表指定的日期和时间的时间。 此表必须包含字段年,月和日,并且可能包含字段小时(默认值为12),分钟(默认值为0),秒(默认值为0)和isdst(默认值为nil)。 有关这些字段的说明,请参见os.date函数。

11

os.tmpname()

返回一个文件名,该文件名可用于临时文件。 文件必须在使用前显式打开,并在不再需要时显式删除。

常见的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 timefor i=1,1000000 doend
-- 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 removea 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

 

Guess you like

Origin www.cnblogs.com/gd-luojialin/p/10962899.html
Recommended