Previous Application Programming: Expressions and Operators Next

Mathematical Operators

IDL mathematical operators are described in the following table.


Note
Also see Assignment and Compound Assignment for information on = and op= and Other Operators for information on the [ ], ( ), and ?: operators.

Table 12-1: Mathematical Operators 

Table 12-1: Mathematical Operators 
Operator
Description
Example
+
Addition
Store the sum of 3 and 6 in B:
B = 3 + 6  
String Concatenation
Store the string value of "John Doe" in B:
B = 'John' + ' ' + 'Doe'  
++
Increment
Adds one to the operand:
A = 3  
A++  
PRINT, A  
IDL Prints:
4  
Note - The increment operator supports both pre- and post-fix syntax. See Using Increment/Decrement.
Subtraction
Store the value of 5 subtracted from 9 in C:
C = 9 - 5  
Negation
Change the sign of C:
C = -C  
--
Decrement
Subtracts one from the operand:
A = 3  
A--  
PRINT, A  
IDL Prints:
2  
Note - The decrement operator supports both pre- and post-fix syntax. See Using Increment/Decrement.
*
Multiplication
Store the product of 2 and 5 in variable C:
C = 2 * 5  
Pointer dereference
If ptr is a valid pointer (created via the PTR_NEW function), then *ptr is the value held by the heap variable that ptr points to. For more information on IDL pointers, see Pointers.
/
Division
Store result of 10.0 divided by 3.2 in variable D:
D = 10.0/3.2  
^
Exponentiation
Store result of 2 raised to the 3rd power in variable B:
B = 2^3  
Note - How exponentiation is evaluated depends upon whether the operands are real or complex. See Using Exponentiation for details.
MOD
Modulo
I MOD J is equal to the remainder when I is divided by J. The magnitude of the result is less than that of J, and its sign agrees with that of I. Print the value of 9 modulo 5:
PRINT, 9 MOD 5  
IDL Prints:
4  
Compute angle modulo 2p.
A =(ANGLE + B) MOD (2 * !PI)  

Using Increment/Decrement

The increment (++) and decrement (--) operators can be applied to variables (including array subscripts or structure tags) of any numeric type. The ++ operator increments the target variable by one. The -- operator decrements the target by one. When written in front of the target variable (that is, using prefix notation), the operations are known as preincrement and predecrement, respectively. When written following the target variable (using postfix notation), they are called postincrement and postdecrement.


Note
The increment and decrement operators can only be applied to variable expressions to which a value can be assigned. Hence, the following is not allowed:

A = 23++

because it attempts to apply the increment operator to a constant. Another way of stating this rule is to say that it must be possible for the expression being incremented or decremented to appear on the left-hand side of the equal sign.

The increment and decrement operators can be used either as standalone statements or within a larger enclosing expression. Although the two forms are very similar, the expression form has some efficiency and side-effect issues (described below) that do not apply to the statement form.

Increment/Decrement Statements

Increment and decrement operators can be used, along with a variable, as standalone statements:

The increment or decrement operator may be placed either before or after the target variable. The same operation is carried out in either case. These operators are very efficient, since the variable is incremented in place and no temporary copies of the data are made.

Increment/Decrement Expressions

Increment and decrement operators can be used within expressions. When the operator follows the target expression, it is applied after the value of the target is evaluated for use in the surrounding expression. When the operator precedes the target expression, it is applied before the value of the target is evaluated for use in the surrounding expression. For example, after executing the following statements, the value of the variable A is 27, while B is 28:

B = 27  
A = B++  

In contrast, after executing the following statements, both A and B have a value of 26:

B = 27  
A = --B  
Efficiency of Prefix vs. Postfix Operations

When used as part of an expression, the prefix form of the increment and decrement operators has an efficiency advantage over the postfix form. The reason for this is that the postfix form requires IDL to make a copy of the data, while the prefix form does not. The operations carried out by IDL to execute a prefix increment or decrement operation are:

  1. Fetch the target variable.
  2.  

  3. Increment or decrement the target variable in place (no copies are made).
  4.  

  5. Use the variable when evaluating the surrounding expression.

This is very efficient. In contrast, the postfix form requires IDL to make a copy of the variable in order to use its old value in the surrounding expression following the increment/decrement. The operations carried out by IDL to execute a postfix increment or decrement operation are:

  1. Fetch the target variable.
  2.  

  3. Make a temporary copy of the variable.
  4.  

  5. Increment or decrement the original variable.
  6.  

  7. Use the temporary copy when evaluating the surrounding expression.

If your computation requires the postfix form, then these operations are necessary and reasonable. If not, the prefix form will use fewer resources and is the better choice. The larger the data involved, the more important this becomes. It is not a concern for small variables.

Order Of Side Effects

The way that the increment and decrement operators change the value of a variable in addition to using its value in a surrounding expression is called a side effect. In most cases, the side effects are desired, and cause no problems. Side effects can cause problems, however, if the increment or decrement operator is applied to a variable that appears more than once within a single statement or expression. Consider the following statement (taken from The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie):

A[i] = i++  

Which value of i is used to index A? Is it the original value of i, or the incremented value? The answer depends on the order in which the various parts of the statement are evaluated. Either answer might be considered correct, and IDL does not require one or the other. Similarly, in the statements

B = 23  
A = B++ + B  

the value of A could be either 47 or 46, depending on which part of the expression is evaluated first.

Note that this situation falls outside the rules of operator precedence — it is the order in which the variables themselves are evalutated that affects the result. Let's examine the situation closely:

As with most languages that implement increment and decrement operators, IDL does not require any particular ordering of evaluation within an expression in which such side effects occur. Different versions or implementations of IDL may evaluate the same expression differently. As a result, you should avoid writing code that depends on a particular ordering of the side effects.

Using Exponentiation

The caret (^) is the exponentiation operator. A^B is equal to A raised to the B power.

For real numbers, A^B is evaluated as follows:

For complex numbers, A^B is evalutated as follows. The complex number A can be represented as A = a + ib, where a is the real part, and ib is the imaginary part. In polar form, we can represent the complex number as A = reiq = r cosq + ir sinq, where r cosq is the real part, and ir sinq is the imaginary part:

  IDL Online Help (March 06, 2007)