Download pbsSoftLogic User Manual

Transcript
pbsSoftLogic User Manual
2014
Lua expression from programming in Lua 3ed written by Roberto ierusalimschy :
Lua supports the usual arithmetic operators: the binary ‘+’ (addition), ‘-’ (subtraction),‘*’ (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
Lua provides the following relational operators:
< > <= >= == ~=
All these operators always produce a boolean value.
The == operator tests for equality; the ~= operator is the negation of equality. We can apply both
operators to any two values. If the values have different types, Lua considers them not equal. Otherwise,
Lua compares them according to their types. Specifically, nil is equal only to itself.
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
Both and and or use short-cut evaluation, that is, they evaluate their second operand only when
necessary. Short-cut evaluation ensures that expressions like (type(v)=="table"and v.tag=="h1") do not
cause run-time errors: Lua will not try to evaluate v.tag when v is not a table.
A useful Lua idiom is x=x or v, which is equivalent to if not x then x = v end That is, it sets x to a default
value v when x is not set (provided that x is not set to false).
Another useful idiom is (a and b)or c, or simply a and b or c, because and has a higher precedence than
or. It is equivalent to the C expression a?b:c, provided that b is not false. For instance, we can select the
maximum of two numbers x and y with a statement like max = (x > y) and x or y When x>y, the first
expression of the and is true, so the and results in its second expression (x), which is always true
(because it is a number), and then the or expression results in the value of its first expression, x. When
x>y is false, the and expression is false and so the or results in its second expression, y.
The not operator always returns a boolean value:
print(not nil) --> true
print(not false) --> true
print(not 0) --> false
print(not not 1) --> true
print(not not nil) --> false
www.pbscontrol.com
Page 99