Welcome to the Harris Geospatial product documentation center. Here you will find reference guides, help documents, and product libraries.


Harris Geospatial / Docs Center / Using IDL / Operator Precedence

IDL

Operator Precedence

Operator Precedence

The following table lists IDL’s operator precedence. Operators with the highest precedence are evaluated first. Operators with equal precedence are evaluated from left to right.

Note: See Efficiency and Expression Evaluation Order for information on creating efficient statements.

Priority

Operator

First (highest)

( ) (parentheses, to group expressions)

[ ] (brackets, to concatenate arrays)

Second

. (structure field dereference)

[ ] (brackets, to subscript an array)

( ) (parentheses, used in a function call)

Third

* (pointer dereference)

^ (exponentiation)

++ (increment)

-- (decrement)

Fourth

* (multiplication)

# and ## (matrix multiplication)

/(division)

MOD (modulus)

Fifth

+ (addition)

- (subtraction and negation)

< (minimum)

> (maximum)

NOT (bitwise negation)

~ (logical negation)

Sixth

EQ (equality)

NE (not equal)

LE (less than or equal)

LT (less than)

GE (greater than or equal)

GT (greater than)

Seventh

AND (bitwise AND)

OR (bitwise OR)

XOR (bitwise exclusive OR)

Eighth

&& (logical AND)

|| (logical OR)

Ninth

?: (conditional expression)

Note: There is also a data type hierarchy that affects the result of mathematical operations. See Data Type and Structure of Expressions for details.

The effect of a given operator is based on both position and the rules of operator precedence. This concept is shown by the following examples.

A = 4 + 5 * 2

A is equal to 14 since the multiplication operator has a higher precedence than the addition operator. Parentheses can be used to override the default evaluation.

A = (4 + 5) * 2

In this case, A equals 18 because the parentheses have higher operator precedence than the multiplication operator; the expression inside the parentheses is evaluated first, and the result is multiplied by two.

Position within the expression is used to determine the order of evaluation when two or more operators share the same operator precedence. Consider the following:

A = 6 / 2 * 3

In this case, A equals 9, since the division operator is to the left of the multiplication operator. The subexpression 6 / 2 is evaluated before the multiplication is done, even though the multiplication and division operators have the same precedence. Again, parentheses can be used to override the default evaluation order:

A = 6 / (2 * 3)

In this case, A equals 1, because the expression inside parentheses is evaluated first.

A useful rule of thumb is, “when in doubt, parenthesize”. Some examples of expressions are provided in the following table.

Expression

Value

A + 1

The sum of A and 1.

A < 2 + 1

The smaller of A or two, plus one.

A < 2 * 3

The smaller of A and six, since * has higher precedence than <.

2 * SQRT(A)

Twice the square root of A.

A + 'Thursday'

The concatenation of the strings A and “Thursday.” An error results if A is not a string

Efficiency and Expression Evaluation Order


The order in which an expression is evaluated can have a significant effect on program speed. Consider the following statement, where A is an array:

; Scale A from 0 to 16.
B = A * 16. / MAX(A)

This statement first multiplies every element in A by 16 and then divides each element by the value of the maximum element. The number of operations required is twice the number of elements in A. A much faster way of computing the same result is used in the following statement:

; Scale A from 0 to 16 using only one array operation.
B = A * (16./MAX(A))

or

; Operators of equal priority are evaluated from left to right. 
; Only one array operation is required.
B = 16./MAX(A) * A 

The faster method only performs one operation for each element in A, plus one scalar division. To see the speed difference on your own machine, execute the following statements:

A = RANDOMU(seed, 512, 512)
t1 = SYSTIME(1) & B = A*16./MAX(A) & t2 = SYSTIME(1)
PRINT, 'Time for inefficient calculation: ', t2-t1
t3 = SYSTIME(1) & B = 16./MAX(A)*A & t4 = SYSTIME(1)
PRINT, 'Time for efficient calculation: ', t4-t3



© 2017 Exelis Visual Information Solutions, Inc. |  Legal
My Account    |    Buy    |    Contact Us