Lua - 數學庫



在科學和工程計算中,我們經常需要數學運算,我們可以使用標準 Lua 庫 math 來實現。math 庫中可用的函式列表如下表所示。

序號 庫/方法及用途
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 為零的情況。)

6

math.ceil (x)

返回大於或等於 x 的最小整數。

7

math.cos (x)

返回 x 的餘弦值(假定以弧度為單位)。

8

math.cosh (x)

返回 x 的雙曲餘弦值。

9

math.deg (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 = m2e,e 是整數,m 的絕對值在 [0.5, 1) 範圍內(或當 x 為零時為零)。

14

math.huge

HUGE_VAL 值,大於或等於任何其他數值的值。

15

math.ldexp (m, e)

返回 m2e(e 應為整數)。

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

π 的值。

22

math.pow (x, y)

返回 xy。(您也可以使用表示式 x^y 來計算此值。)

23

math.rad (x)

返回以度數表示的角度 x 的弧度值。

24

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

此函式是 ANSI C 提供的簡單偽隨機生成器函式 rand 的介面。在不帶引數呼叫時,返回範圍 [0,1) 內的均勻偽隨機實數。當使用整數 m 呼叫時,math.random 返回範圍 [1, m] 內的均勻偽隨機整數。當使用兩個整數 m 和 n 呼叫時,math.random 返回範圍 [m, n] 內的均勻偽隨機整數。

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 的雙曲正切值。

三角函式

下面顯示了一個使用三角函式的簡單示例。

main.lua

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

其他常用數學函式

下面顯示了一個使用常用數學函式的簡單示例。

main.lua

-- 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

以上示例只是一些常見示例,我們可以根據需要使用 math 庫,因此請嘗試使用所有函式以更熟悉它們。

廣告
© . All rights reserved.