メインコンテンツまでスキップ

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^yx の 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
logx の自然対数を計算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
ldexpx * (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