Lua 3 Expressions

3.1
Arithmetic Operators
Lua supports the usual arithmetic operators: the binary ‘ + ’ (addition), ‘ - ’ (sub-
traction), ‘ * ’ (multiplication), ‘ / ’ (division), ‘ ^ ’ (exponentiation), ‘ % ’ (modulo), and
the unary ‘ - ’ (negation). All of them operate on real numbers. For instance,
x^0.5 computes the square root of x , while x^(-1/3) computes the inverse of its
cubic root.
The following rule defines the modulo operator:
a % b == a - math.floor(a/b)*b
For integer operands, it has the usual meaning, with the result always having
the same sign as the second argument. For real operands, it has some extra
uses. For instance, x%1 is the fractional part of x , and so x - x%1 is its integer
part. Similarly, x - x%0.01 is x with exactly two decimal digits:
x = math.pi
print(x - x%0.01)
–> 3.14

3.2 Relational Operators

Lua provides the following relational operators:

< >  <=  >=  ==  ~=

3.3 Logical Operators

The logical operators are and, or, and not. Like control structures, all logical
operators consider both the boolean false and nil as false, and anything else
as true. The and operator returns its first argument if it is false; otherwise, it
returns its second argument. The or operator returns its first argument if it is
not false; otherwise, it returns its second argument:
print(4 and 5) –>5
print(nil and 13) –>nil
print(false and 13) –>false
print(4 or 5) –>4
print(false or 5) –>5

x = x or v <==> if not x then x = v end
max = (x > y) and x or y <==> a?b:c

猜你喜欢

转载自blog.csdn.net/u013420428/article/details/80415028
今日推荐