Operators
Arithmetic operators
There are several arithmetic operators that Lua offers out of the box. These are pretty common among computational tools (e.g. excel), so there might be nothing really new here for you.
-- operators.lua
-- ===========
-- Tell Emp that we do not intend to solve anything
auto_solve = false
-- Define two variables
a = 2
b = 3
-- Addition
print(a.." + "..b.." = "..a+b)
-- Substraction
print(a.." - "..b.." = "..a-b)
-- Multiplication
print(a.." * "..b.." = "..a*b)
-- Float division
print(a.." / "..b.." = "..a/b)
-- Floor division
-- According to Lua's documentation, this is:
--[[
"a division that rounds the quotient towards
minus infinity, that is, the floor of the
division of its operands"
That is, it always returns an integer
]]
print(a.." // "..b.." = "..a//b)
-- Modulo
print(a.." % "..b.." = "..a%b)
-- Exponentiation
print(a.." ^ "..b.." = "..a^b)
-- Unary minus
print(" (-"..b..") = "..-b)And the result will be
Relational operators
In addition to add or substract, it will sometimes be relevant to compare values (not just numbers). In order to do this, Lua offers the relational operators. These operators always return true or false (i.e. a boolean) and never other kind of value. The available operators are:
Which results in
Logical operators
Logical operators are useful for evaluating more complex expressions than any relational operation allow. For example, 'A = true if B AND C are both true'
The relational operators in Lua are or, and and not. These are pretty straightforward, as they are self-explanatory.
These operators can be nested and combined with relational operators, and used for defining new variables. For instance:
When assigning variables, it is relevant to consider which value do the three operators return. The Lua documentation states that:
The
notoperator always returnstrueorfalse.The
andoperator returns the first argument if it is equals tonilorfalse, otherwise it returns the second argumentThe
oroperator returns the first argument if this is different fromnilorfalse; otherwise, returns the second argument.
Other operators
There are a few other operators that will be useful for Lua users in Emp. These are the length (#), and concatenation (..) operators.
The length operator, as its name states, usually returns the 'length' of the object that it is applied to. It can only be applied to Strings (returning the number of characters in it) and Tables (returning the last numeric key in it). A consecuence of the behavior of the length operator in Tables is that, when they are created without explicit keys (i.e. Arrays), it will return the real length of it.
This behavior may look a little strange and, in fact, I do not know the design reasons behind this. However, it is a reasonable behaviour as the length of a table tells you the maximum key you can use for getting a value (i.e. another_table[1] does exist, while another_table[2] does not).
The Concatenate operator puts together two variables of type String or Number into a single String.
Last updated