Combining arithmetic expressions
Quick reference
To combine arithmetic expressions, type:
variable operator variable [operator variable ... ]
where variable is a numeric value or the name of a variable containing a numeric value, and operator is one of the arithmetic operators +, -, * (multiply) or / (divide).
More information
You often need to combine numeric expressions to form a larger expression, for example to count the number of records read with a given code in a named column.
Arithmetic operators
Arithmetic expressions are linked with any of the arithmetic operators listed below:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
Expressions may contain more than one of these operators, for example:
t5 + c(134,136) / otot
c(150,152) * 10 + 2.5
Order of evaluation
Quantum evaluates such expressions in the following order:
1 Expressions in parentheses
2 Multiplication and division
3 Addition and subtraction
To change this order, enclose the expressions which go together in parentheses. The first expression in the example above is evaluated by dividing the value in columns 134 to 136 by otot and adding the result to t5. If you change the expression to:
(t5 + c(134,136)) / otot
this first adds the values of t5 and c(134,136), and then divides that by otot. Substitute numbers and compare the results. If t5=10, otot=5 and the value in c(134,136) is 125, the two versions of the expression read as follows:
10 + 125 / 5 = 35
and
(10 + 125) / 5 = 27
Where two integer expressions are combined, the result is integer (any decimal places are ignored), but if an expression contains a real, the result is real. Therefore, if t1=5 and t2=3, then:
t1 + 4 | = 9 |
t1 + 4.0 | = 9.0 |
t1 * t2 | = 15 |
t1 / t2 | = 1 |
t1 * 1.0 | = 5.0 |
t1 * 1.0 / t2 | = 1.66667 |
If you use parentheses in expressions which contain both integer and real variables, you must take care to ensure that your expression is producing the correct results.
Example of using parentheses
This example shows how an expression can look correct but can still produce unexpected results.
If t40=2 and t41=70, the expression:
t40 * 100.0 / t41
gives a result of 2.85714 (that is, 200.0/70). The final value is 2.85714 if the result is saved in a real variable, or 2 if it is saved in an integer variable.
If you use parentheses:
(t40 / t41) * 100.0
the result is 0.0 (or 0 if saved in an integer variable). This is because Quantum evaluates expressions in parentheses before it deals with the rest of the expression; it treats that expression as integer arithmetic. The rules for integer arithmetic dictate that real results are truncated at the decimal point, so the true result of 0.0285714 becomes 0. Any multiplication involving zero is always zero, so the final result is zero.
If you find that a run gives unexpected zero results, look for expressions of this type and checking whether the parenthesized part of the expression has been truncated because the integer division results in a decimal number.
See also