跳至主要內容

math 庫的相關應用

函式名描述範例結果
pi圓周率math.pi3.1415926535898
abs取絕對值math.abs(-2012)2012
ceil向上取整math.ceil(9.1)10
floor向下取整math.floor(9.9)9
max取參數最大值math.max(2,4,6,8)8
min取參數最小值math.min(2,4,6,8)2
x^y計算 x 的 y 次冪(Lua 5.3 起 math.pow 已移除,改用 ^ 運算符)2^1665536.0
sqrt開平方math.sqrt(65536)256.0
modf取整數和小數部分math.modf(20.12)20 0.12
randomseed設隨機數種子math.randomseed(os.time())
random取隨機數math.random(5,90)5~90
rad角度轉弧度math.rad(180)3.1415926535898
deg弧度轉角度math.deg(math.pi)180.0
expe的x次方math.exp(4)54.598150033144
log計算 x 的自然對數math.log(54.598150033144)4.0
log (指定底數)計算以指定底數的對數(Lua 5.3 起 math.log10 已移除,改用 math.log(x, 10)math.log(1000, 10)3.0
frexp將參數拆成x * (2 ^ y)的形式math.frexp(160)0.625 8
ldexp計算x * (2 ^ y)math.ldexp(0.625,8)160.0
sin正弦math.sin(math.rad(30))0.5
cos余弦math.cos(math.rad(60))0.5
tan正切math.tan(math.rad(45))1.0
asin反正弦math.deg(math.asin(0.5))30.0
acos反余弦math.deg(math.acos(0.5))60.0
atan反正切math.deg(math.atan(1))45.0