Lua-math-library
Lua-数学ライブラリ
科学計算や工学計算では数学演算が必要になることが多く、標準のLuaライブラリ数学を使用してこれを利用できます。 数学ライブラリで使用可能な関数のリストを次の表に示します。
Sr.No. | Library/Method & Purpose |
---|---|
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 power xを返します。 |
11 |
math.floor (x) x以下の最大の整数を返します。 |
12 |
math.fmod (x, y) 商をゼロに丸めるxをyで割った余りを返します。 |
13 |
math.frexp (x) x = m2e、eが整数で、mの絶対値が範囲[0.5、1)(またはxがゼロの場合はゼロ)になるようなmとeを返します。 |
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の小数部の2つの数値を返します。 |
21 |
math.pi 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]の範囲の一様な擬似乱数整数を返します。 2つの整数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の双曲線正接を返します。 |
三角関数
三角関数を使用した簡単な例を以下に示します。
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
その他の一般的な数学関数
一般的な数学関数を使用した簡単な例を以下に示します。
-- 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
上記の例は一般的な例のほんの一部です。必要に応じて数学ライブラリを使用できます。すべての関数を使用して、より慣れてください。