3.4.1 – Arithmetic Operators
Lua supports the usual arithmetic operators: the binary + (addition), - (subtraction), * (multiplication), /
(division), % (modulo), and ^ (exponentiation); and unary - (mathematical negation). If the operands are
numbers, or strings that can be converted to numbers (see §3.4.2), then all operations have the usual
meaning. Exponentiation works for any exponent. For instance, x^(-0.5) computes the inverse of the
square root of x. Modulo is defined as
a % b == a - math.floor(a/b)*b
That is, it is the remainder of a division that rounds the quotient towards minus infinity.
3.4.2 – Coercion
Lua provides automatic conversion between string and number values at run time. Any arithmetic
operation applied to a string tries to convert this string to a number, following the rules of the Lua lexer.
(The string may have leading and trailing spaces and a sign.) Conversely, whenever a number is used
where a string is expected, the number is converted to a string, in a reasonable format. For complete
control over how numbers are converted to strings, use the format function from the string library (see
string.format).
3.4.3 – Relational Operators
The relational operators in Lua are
== ~= < > <= >=
These operators always result in false or true.
Equality (==) first compares the type of its operands. If the types are different, then the result is false.
Otherwise, the values of the operands are compared. Numbers and strings are compared in the usual
way. Tables, userdata, and threads are compared by reference: two objects are considered equal only if
they are the same object. Every time you create a new object (a table, userdata, or thread), this new
object is different from any previously existing object. Closures with the same reference are always
equal. Closures with any detectable difference (different behavior, different definition) are always
different.
You can change the way that Lua compares tables and userdata by using the "eq" metamethod (see
§2.4).
The conversion rules of §3.4.2 do not apply to equality comparisons. Thus, "0"==0 evaluates to false,
and t[0] and t["0"] denote different entries in a table.
The operator ~= is exactly the negation of equality (==).
The order operators work as follows. If both arguments are numbers, then they are compared as such.
Otherwise, if both arguments are strings, then their values are compared according to the current locale.
Otherwise, Lua tries to call the "lt" or the "le" metamethod (see §2.4). A comparison a > b is translated
to b < a and a >= b is translated to b <= a.
3.4.4 – Logical Operators
The logical operators in Lua are and, or, and not. Like the control structures (see §3.3.4), all logical
operators consider both false and nil as false and anything else as true.
The negation operator not always returns false or true. The conjunction operator and returns its first
argument if this value is false or nil; otherwise, and returns its second argument. The disjunction
operator or returns its first argument if this value is different from nil and false; otherwise, or returns its
second argument. Both and and or use short-cut evaluation; that is, the second operand is evaluated
only if necessary. Here are some examples:
10 or 20 --> 10
10 or error() --> 10
1