Arithmetic Expressions

Table 23-22 documents the order in which elements of an arithmetic expression are evaluated (i.e. the order of precedence).  The operations are presented in their order of precedence starting with the highest precedence, that is, those elements that are evaluated first.  For operators that have an equal precedence, elements are evaluated left-to-right.  Parentheses can be used to change the order of evaluation.  Operations within parentheses are always evaluated before operations that are outside of the parentheses.

Table 23-22: Arithmetic Expressions, Order of Precedence

Operation Symbol Notes

Exponentiation

^

Raises a value by a specified power.  For example “x ^ 3” cubes the value of x.  Powers have to be integer numbers if the number being operated on is negative.  Otherwise, powers can have fractional parts.

Unary negation

-

This is a negative sign in front of a variable or constant that does not indicate a subtraction operation.  For example, 2 * -4 is valid and produces a value of –8.

Multiplication/division

*,  /

This indicates the standard multiplication and division operations.  For division, even if the divisor and the dividend are integer values, the result is computed as a real number with a fractional part.

Integer division

\

This indicates an integer division operation.  Independent of the data type for the divisor and dividend, the result is truncated to an integer number.  For example, “2.3 \ 2” yields a value of 1.

Modulus calculation

Mod

Computes the modulus of two numbers.  For “x Mod y”, this is equivalent to dividing x by y and returning the remainder.  For example, “13.3 Mod 2” is equal to 1.3.

Addition/subtraction

+-

Standard addition and subtraction operations.  Automatically converts integer values to floating point and computes the result in floating point.  If the value is stored into an integer variable type, the resulting answer is converted to integer before storage.

String concatenation

+ or &

Either of the two symbols can be used to indicate string concatenation. However, the use of "&" is preferred in place of "+" to clearly specify a string concatenation operation instead of numerical addition.

Arithmetic bit shift

<<>>

These are arithmetic shift operations and not bit rotations or logical shifts.  For left shifts, a 0 is always shifted into the low-order bit.  For right shifts, for positive numbers a 0 is shifted into the high-order bit and a 1 is shifted in for negative numbers. 

Relational comparisons

=, <>, <, >, <=, >=, Is

The first six relational operator symbols represent “equal to”, “not equal to”, “less than”, “greater than”, “less than or equal to”, and “greater than or equal to”. The operands to the left and right of these relational operators can either both be numerical or string values.  The Is operator determines if two object references refer to the same object. For example "object1 Is Nothing".

Logical NOT

Not

Converts a False (=0) value to True (-1) and any True (<>0) value to False (0).

Logical or bitwise AND

And, AndAlso

Performs a logical AND operation unless either of the operands is not a Boolean value, in which case, a bitwise operation is performed.  All operands of And are always evaluated even if an earlier operand has already decided the result.  AndAlso prematurely stops evaluation if the result is already False.  The following illustrates the logical AND operation:

True And True -> True
True And False -> False
False And True -> False
False And False -> False

Logical or bitwise OR

Or, OrElse

Performs a logical OR operation unless either of the operands is not a Boolean value, in which case, a bitwise operation is performed.  All operands of Or are always evaluated even if an earlier operand has already decided the result.  OrElse prematurely stops evaluation if the result is already True.  The following illustrates the logical OR operation:

True Or True -> True
True Or False -> True
False Or True -> True
False Or False -> False

Logical or bitwise XOR

Xor

Performs a logical Exclusive Or operation unless either of the operands is not a Boolean value, in which case, a bitwise operation is performed.  The following illustrates the logical XOR operation:

True Xor True -> False
True Xor False -> True
False Xor True -> True
False Xor False -> False

5.1.1. In general, most arithmetic expressions evaluation with GPL is performed in double precision floating point.  For example, when two numbers are added together, they are first converted to Double’s if necessary and then the addition operation is performed.  The results of expressions are converted to the appropriate data types when a variable is assigned a value.  Because of this, GPL generally executes more quickly when variables are declared as Double’s than the other types of numeric values.