Difference between revisions of "Expressions"
(→Order of operations) |
|||
Line 1: | Line 1: | ||
− | <div style="float: right; margin-left: 1em; margin-bottom: 1em;">__TOC__</div> | + | <div style="float: right; clear: both; margin-left: 1em; margin-bottom: 1em;">__TOC__</div> |
==Introduction== | ==Introduction== | ||
An expression is a complex mathematical construction built using operands and operators. The simplest of expressions can have one operand, returning its value: <code>6</code> or <code>$now</code>. Generally, expressions contain multiple operands separated by operators. The operands represent the values, while the operators provide the operations to be performed on these values. Here's a simple example of a two-operand expression: <code>6 + 4</code>. In this example, there are two operands, <code>6</code> and <code>4</code> and one operator, <code>+</code>, the mathematical addition operator. The result of this expression is obviously 10. | An expression is a complex mathematical construction built using operands and operators. The simplest of expressions can have one operand, returning its value: <code>6</code> or <code>$now</code>. Generally, expressions contain multiple operands separated by operators. The operands represent the values, while the operators provide the operations to be performed on these values. Here's a simple example of a two-operand expression: <code>6 + 4</code>. In this example, there are two operands, <code>6</code> and <code>4</code> and one operator, <code>+</code>, the mathematical addition operator. The result of this expression is obviously 10. |
Revision as of 16:45, 6 May 2017
Contents
- 1 Introduction
- 2 Using an expression
- 3 Values versus Expressions
- 4 Expression operands
- 5 Expression operators
- 5.1 Arithmetic operators
- 5.2 Bitwise operators
- 5.3 Logical operators
- 5.3.1 Logical AND (a && b)
- 5.3.2 Logical OR (a || b)
- 5.3.3 Logical XOR (a ^^ b)
- 5.3.4 Logical negation (!b)
- 5.3.5 Logical double-negation (!!b)
- 5.3.6 Logical NAND (a !& b)
- 5.3.7 Logical NOR (a !| b)
- 5.3.8 Logical XNOR (a !^ b)
- 5.3.9 Equality (a == b)
- 5.3.10 Inequality (a != b)
- 5.3.11 Less than (a < b)
- 5.3.12 Less than or equal to (a <= b)
- 5.3.13 Greater than (a > b)
- 5.3.14 Greater than or equal to (a >= b)
- 5.4 Special operators
- 6 Order of operations
- 7 Logical truth table
Introduction
An expression is a complex mathematical construction built using operands and operators. The simplest of expressions can have one operand, returning its value: 6
or $now
. Generally, expressions contain multiple operands separated by operators. The operands represent the values, while the operators provide the operations to be performed on these values. Here's a simple example of a two-operand expression: 6 + 4
. In this example, there are two operands, 6
and 4
and one operator, +
, the mathematical addition operator. The result of this expression is obviously 10.
Using an expression
When building a webCoRE piston, there are several ways values can be provided within piston operands. The operand type selection allows for several types, among which the Value and the Expression types. When a Value type is selected, a Value field is displayed right next to the operand type selector. When an Expression type is selected, an Expression field is displayed underneath the selector and provides multiple line editing and autocomplete suggestions. Expressions can be used directly in an Expression field, but they can also be embedded into Value fields, by using the curly brackets {<expression>}
. This allows for quick use of expressions while building, say, an output string:
The sum of 6 and 4 is {6 + 4
}
webCoRE will parse anything that is between curly brackets as expressions and handle them as such.
Values versus Expressions
Which one you end up using is really a matter of choice, they can both do the same exact thing, although they differ from each-other. A Value field is an output-driven field, meaning it makes it easy to build output strings that need to be displayed in logs, notifications, etc. Think of a Value field like an Expression field that has implied double quotes (") at the start and end. In reverse, an Expression field is similar to a Value field that has curly brackets around it. Here's an example:
- We can convert this Value field :
The sum of 6 and 4 is {6 + 4}
into an Expression field by simply placing double quotes around it:"The sum of 6 and 4 is {6 + 4}"
- We can convert this Expression field :
"The sum of 6 and 4 is " + (6 + 4)
into a Value field by simply placing curly brackets around it:{"The sum of 6 and 4 is " + (6 + 4)}
All four examples above are equivalent and will output the same result: The sum of 6 and 4 is 10
Expression operands
Expression operands come in different types:
Literal strings
- contain strings, pieces of text that will be output as-is, like that
"The sum of 6 and 4 is "
above
Numeric values
- anything like integer numbers or decimal numbers:
6
or2.4
Variables
- the name of any defined variable, local, system, or global, can be simply written and the expression will know to replace that with its actual value, for example
$now
Functions
- webCoRE allows functions in expressions, these can help convert values around and extend the power of expressions, for example
dewPoint(77, 80)
, orround(6.24)
Device attributes
- use the special construction
[name:attribute]
or[variable:attribute]
to include the attribute value of that device.
Devices
- use the special construction
[name]
or[variable]
to point to a device. This is not a device attribute, but the device itself and when converted to string will give the device's name
Expression operators
webCoRE supports over 30 operators, with various meanings. Operators generally sit in between two operands and determines the operation that is performed on the two. When only one operand exists, the other may be automatically implied, as described below. Another important factor is the order of operations, also explained below.
Warning: | For each of the operators explained below, we are assuming we have two operands around it, a operator b . For simplicity, we will be referring to the two operands simply as a and b . Note that some operators only accept one operand, in which case, they apply to the immediately following operand, or b in our notation. |
Arithmetic operators
Addition (a + b
)
Performs the mathematical addition of a
plus b
. If any of a
or b
is a string, concatenation is performed, so "test" + 1
will result in test1
, while 6 + 4
will result in 10
. If a
and b
are device-typed variables, the result will be a new list with all the devices present in either a
or b
.
Subtraction (a - b
)
Performs the mathematical subtraction of a
minus b
. If a
and a
are device-typed variables, the result will be the list of devices in a
, but excluding any devices that exist in b
. Otherwise, both a
and b
will be evaluated as numbers and subtracted.
Multiplication (a * b
)
Performs the mathematical multiplication of a
times b
. Both a
and b
will be evaluated as numbers and multiplied.
Division (a / b
)
Performs the mathematical division of a
divided by b
. Both a
and b
will be evaluated as numbers and multiplied. If any of the two operands is a decimal, the result will be a decimal. If both operands are integers, the result will be an integer.
Integer division (a \ b
)
Performs the mathematical division of a
divided by b
. Both a
and b
will be evaluated as numbers and divided, then the integer part of the result will be returned.
Modulo (a % b
)
Performs the mathematical modulo operation of a
modulo b
. Both a
and b
will be evaluated as numbers and divided, then the remainder of the division will be returned.
Exponential (a ** b
)
Performs the mathematical exponential operation of a
at the power of b
. Both a
and b
will be evaluated as numbers and divided, and a^{b}
will be returned.
Bitwise operators
Bitwise AND (a & b
)
Performs the bitwise AND operation between a
and b
. Both a
and b
will be evaluated as integers and a bitwise AND will be applied between them. See AND for more details.
Bitwise OR (a | b
)
Performs the bitwise OR operation between a
and b
. Both a
and b
will be evaluated as integers and a bitwise OR will be applied between them. See OR for more details.
Bitwise XOR (a ^ b
)
Performs the bitwise XOR operation between a
and b
. Both a
and b
will be evaluated as integers and a bitwise XOR will be applied between them. See XOR for more details.
Bitwise NOT (~b
)
Performs the bitwise NOT operation on the immediately following operand. The operand will be evaluated as an integer and a bitwise NOT will be applied to it. See NOT for more details.
Bitwise NAND (a ~& b
)
Performs the bitwise NAND operation between a
and b
. Both a
and b
will be evaluated as integers and a negated bitwise AND will be applied between them.
Bitwise NOR (a ~| b
)
Performs the bitwise NOR operation between a
and b
. Both a
and b
will be evaluated as integers and a negated bitwise OR will be applied between them.
Bitwise XNOR (a ~^ b
)
Performs the bitwise XNOR operation between a
and b
. Both a
and b
will be evaluated as integers and a negated bitwise XOR will be applied between them.
Bitwise left shift (a << b
)
Performs the bitwise left shift operation, shifting a
to the left by b
bits. The operands will be evaluated as an integers and the result will be an integer. See Bitwise shift operators for more details.
Bitwise right shift (a >> b
)
Performs the bitwise rigth shift operation, shifting a
to the right by b
bits. The operands will be evaluated as an integers and the result will be an integer. See Bitwise shift operators for more details.
Logical operators
Logical AND (a && b
)
Performs the logical AND operation between a
and b
. Both a
and b
will be evaluated as booleans and the result will be true only when both operands evaluate as true.
Logical OR (a || b
)
Performs the logical OR operation between a
and b
. Both a
and b
will be evaluated as booleans and the result will be true when any of the two operands evaluate as true.
Logical XOR (a ^^ b
)
Performs the logical XOR operation between a
and b
. Both a
and b
will be evaluated as booleans and the result will be true only when one of the two operands evaluates as true and the other one evaluates as false.
Logical negation (!b
)
Performs the logical negation operation on the immediately following operand. The operand will be evaluated as boolean and the result will be true if the operand evaluates as false.
Logical double-negation (!!b
)
Converts the immediately following operand into a boolean. The operand will be evaluated as boolean and returned as is.
Logical NAND (a !& b
)
Performs the logical NAND operation between a
and b
. Both a
and b
will be evaluated as booleans and the result will be false only when both operands evaluate as true.
Logical NOR (a !| b
)
Performs the logical NOR operation between a
and b
. Both a
and b
will be evaluated as booleans and the result will be false when any of the two operands evaluate as true.
Logical XNOR (a !^ b
)
Performs the logical XNOR operation between a
and b
. Both a
and b
will be evaluated as booleans and the result will be false only when one of the two operands evaluates as true and the other one evaluates as false.
Equality (a == b
)
Returns true if a
is equal to b
. Depending on the operand data types, one or the other, or even both, may have to be converted to a common data type before comparison is performed.
Inequality (a != b
)
Returns true if a
is NOT equal to b
. Depending on the operand data types, one or the other, or even both, may have to be converted to a common data type before comparison is performed. An alias of the inequality operator is <>
Less than (a < b
)
Returns true if a
is less than b
. Depending on the operand data types, one or the other, or even both, may have to be converted to a common data type before comparison is performed. It can natively compare numbers and strings.
Less than or equal to (a <= b
)
Returns true if a
is less than or equal to b
. Depending on the operand data types, one or the other, or even both, may have to be converted to a common data type before comparison is performed. It can natively compare numbers and strings.
Greater than (a > b
)
Returns true if a
is greater than b
. Depending on the operand data types, one or the other, or even both, may have to be converted to a common data type before comparison is performed. It can natively compare numbers and strings.
Greater than or equal to (a >= b
)
Returns true if a
is greater than or equal to b
. Depending on the operand data types, one or the other, or even both, may have to be converted to a common data type before comparison is performed. It can natively compare numbers and strings.
Special operators
Parenthesis ( .. )
Not an operator per-se, parenthesis are used to group operands and operators together to alter the normal order of operations. For example, 6 + 4 * 2
returns 14, but if we really need to find the double of 6 + 4, then we write it like this (6 + 4) * 2
. Parenthesis and curly brackets are interchangeable, you can use either or, but ensure you close them with the same style: {(6 + 4) * 2} ** 2
Ternary operator ( c ? t : f )
A great tool to simplifying expressions, the ternary operator evaluates c
as a boolean, then if that evaluation is true, returns t
, otherwise returns f
.
Order of operations
1 | () {} func() | Parenthesis, curly brackets, function calls |
2 | ! !! ~ | Logical negation, logical double-negation and bitwise NOT unary operators |
3 | ** | Exponent operator |
4 | * / \ % | Multiplication, division, modulo |
5 | + - | Addition and subtraction |
6 | << >> | Shift left and shift right operators |
7 | < <= > >= | Comparisons: less than, less than or equal to, greater than, greater than or equal to |
8 | == != | Comparisons: equal and not equal |
9 | & ~& | Bitwise AND and bitwise NAND operators |
10 | ^ ~^ | Bitwise exclusive OR (XOR) and bitwise XNOR operators |
11 | | ~| | Bitwise inclusive (normal) OR and bitwise NOR operators |
12 | && !& | Logical AND and logical NAND operators |
13 | ^^ !^ | Logical XOR and logical XNOR operators |
14 | || !| | Logical OR and logical NOR operators |
15 | ? : | Ternary operators (conditional expression) |
Logical truth table
a | b | a AND b | a NAND b | a OR b | a NOR b | a XOR b | a XNOR b |
---|---|---|---|---|---|---|---|
T | T | T | F | T | F | F | T |
T | F | F | T | T | F | T | F |
F | T | F | T | T | F | T | F |
F | F | F | T | F | T | F | T |