1. 基礎庫
我們在整個教程中使用了各種主題下的基本庫。 下表提供了相關頁面的鏈接,並列出了本Lua教程各部分所涵蓋的功能。
| 編號 |
庫/方法 |
作用 |
| 1 |
錯誤處理 |
包括錯誤處理函數,如斷言, 錯誤,如Lua錯誤處理中所述。 |
| 2 |
內存管理 |
包括與垃圾收集相關的自動內存管理功能, 如Lua垃圾收集中所述。 |
| 3 |
|
它打開文件並以塊的形式執行文件的內容。 |
| 4 |
|
因此是保存全局環境的全局變量(即 |
| 5 |
|
返回函數使用的當前環境。 |
| 6 |
|
如果 |
| 7 |
|
此函數獲取表的索引和值。 |
| 8 |
|
使用函數 |
| 9 |
|
與 |
| 10 |
|
與 |
| 11 |
|
允許程序遍歷表的所有字段。 |
| 12 |
|
暫停正在運行的協同程序。 |
| 13 |
|
打印給定的參數值。 |
| 14 |
|
檢查 |
| 15 |
|
獲取 |
| 16 |
|
將 |
| 17 |
|
如果 |
| 18 |
|
設置給定函數使用的環境。 |
| 19 |
|
設置給定表的元表。 |
| 20 |
|
嘗試將參數轉換為數字。 |
| 21 |
|
接收任何類型的參數並將其轉換為合理格式的字符串。 |
| 22 |
|
返回唯一參數的類型,編碼為字符串。 |
| 23 |
|
返回給定表中的元素。 |
| 24 |
|
包含當前解釋器版本的字符串的全局變量(不是函數)。 |
| 25 |
協同程序 |
包括Lua協同程序中解釋的協程操作功能。 |
2. Lua數學庫
| 編號 |
庫或方法 |
描述 |
| 1 |
|
返回 |
| 2 |
|
返回 |
| 3 |
|
返回 |
| 4 |
|
返回 |
| 5 |
|
返回 |
| 6 |
|
返回大於或等於 |
| 7 |
|
返回 |
| 8 |
|
返回 |
| 9 |
|
以度為單位返回角度 |
| 10 |
|
返回值 |
| 11 |
|
返回小於或等於 |
| 12 |
|
返回 |
| 13 |
|
返回 |
| 14 |
|
|
| 15 |
|
返回 |
| 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.00.01.0180
其他常見的數學函數
使用常見數學函數的簡單示例如下所示-
-- 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")
當運行上面的程序時,將得到類似的輸出如下 -
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
上面的例子只是一些常見的例子,可根據自己的需要使用OS庫,建議嘗試使用所有的功能以便更加熟悉。像remove這樣的函數有助於刪除文件,執行有助於於執行OS命令。
--------------------------------------------------
轉載於:https://www.yiibai.com/lua/lua_operating_system_facilities.html
