Lua數學庫

1

`math.abs(x)`

2

`math.acos(x)`

3

`math.asin(x)`

4

`math.atan(x)`

5

`math.atan2(y,x)`

6

`math.ceil(x)`

7

`math.cos(x)`

8

`math.cosh(x)`

9

`math.deg(x)`

10

`math.exp(x)`

11

`math.floor(x)`

12

`math.fmod(x,y)`

13

`math.frexp(x)`

14

`math.huge`

`HUGE_VAL`值是一個大於或等於任何其他數值的值。

15

`math.ldexp(m, e)`

16

`math.log(x)`

17

`math.log10(x)`

18

`math.max(x,...)`

19

`math.min(x,...)`

20

`math.modf(x)`

21

`math.pi`

`pi`的值。

22

`math.pow(x,y)`

23

`math.rad(x)`

24

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

25

`math.randomseed(x)`

`x`設置爲僞隨機生成器的「種子」：相等的種子產生相等的數字序列。

26

`math.sin(x)`

27

`math.sinh(x)`

28

`math.sqrt(x)`

29

`math.tan(x)`

30

`math.tanh(x)`

三角函數

``````radianVal = math.rad(math.pi / 2)

-- Sin value of 90(math.pi / 2) degrees

-- Cos value of 90(math.pi / 2) degrees

-- Tan value of 90(math.pi / 2) degrees

-- Cosh value of 90(math.pi / 2) degrees

-- Pi Value in degrees
io.write(math.deg(math.pi),"\n")``````

``````0.027415567780804
0.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``````

0 條評論，你可以發表評論，我們會進行改進