「Lua入門」算術演算子を使うサンプル

環境
Lua 5.4.4

構文

Luaの算術演算子には、+(加算)、 -(減算)、 *(積算)、 /(除算) の四則演算子と ^(累乗)、%(剰余)があります。
形式一覧
+ 加算 – 減算
* 積算 / 除算
^ 累乗 % 剰余

使用例

a = 32
b = 10
c = a + b
print("Line 1 - c value is ", c )
c = a - b
print("Line 2 - c value is ", c )
c = a * b
print("Line 3 - c value is ", c )
c = a / b
print("Line 4 - c value is ", c )
c = a % b
print("Line 5 - c value is ", c )
c = a^2
print("Line 6 - c value is ", c )
c = -a
print("Line 7 - c value is ", c )

実行結果

C:\study\lua>lua test.lua
Line 1 - c value is     42
Line 2 - c value is     22
Line 3 - c value is     320
Line 4 - c value is     3.2
Line 5 - c value is     2
Line 6 - c value is     1024.0
Line 7 - c value is     -32

 

Lua

Posted by arkgame