Previous Application Programming: Expressions and Operators Next

Relational Operators

The IDL relational operators apply a relation to two operands and return a logical value of true or false. The resulting logical value can be used as the predicate in IF, WHILE or REPEAT statements. You can also combine Boolean operators with other logical values to make more complex expressions.


Note
It is important to see Definition of True and False for details on when a value is considered true or false.

The rules for evaluating relational expressions with operands of mixed modes are the same as for arithmetic expressions. Each operand is promoted to the data type of the operand with the greatest precedence or potential precision. (See Data Type and Structure of Expressions for details.) For example, in the relational expression "2 EQ 2.0", the integer 2 is converted to floating point and compared to the floating point 2.0. The result of this expression is true. The relational operators return a value of 1 for true and 0 for false. The type of the result is always byte.


Note
When using EQ and NE with complex numbers, both the real and imaginary parts must meet the condition of the relational operator. For example, the following returns 0 (false):
   PRINT, COMPLEX(1,2) EQ COMPLEX(1,-2)

When using GE, GT, LE, and LT with complex numbers, the absolute value (or modulus) of the complex number is used for the comparison.

For more information on using relational operators, also see Using Relational Operators with Arrays and Relational Operators with Infinity and NaN Values.

Table 12-6: Relational Operators 

Table 12-6: Relational Operators 
Operator
Description
Example
EQ
Equal to
Returns true if its operands are equal; otherwise, it returns false. The following returns True:
IF (2 EQ 2.0) THEN PRINT, 'True'  
NE
Not equal to
Returns true whenever the operands are different. The following returns 1 (true):
PRINT, "sun" NE "fun"  
GE
Greater than or equal to
Returns true if the operand on the left is greater than or equal to the one on the right. Relational operator are useful for creating array masks:
A = ARRAY * (ARRAY GE 100)  
See Using Relational Operators with 
Arrays.  
GT
Greater than
Returns true if the operand on the left is greater than the operand on the right. Determine if A is greater than B:
IF (A GT B) THEN PRINT, 'True'  
Note - Strings are compared using the ASCII collating sequence: " " is less than "0" is less than "9" is less than "A" is less than "Z" is less than "a" which is less than "z".
LE
Less than or equal to
Returns true if the operand on the left is less than or equal to the operand on the right. Determine if A is less than or equal to B:
IF (A LE B) THEN PRINT, 'True'  
LT
Less than
Returns true if the operand on the left is less than the operand on the right. Determine if A is less than B:
IF (A LT B) THEN PRINT, 'True'  


Note
You can use the NE and EQ operators to determine if two object references point to the same heap variable. See Object Equality and Inequality for examples.

Using Relational Operators with Arrays

Relational operators can be applied to arrays, and the resulting array of ones and zeroes can be used as an operand. For example, the expression:

A = ARR * (ARR LE 100)  

A is an array equal to ARR except that all points greater than 100 have been reduced to zero. The expression (ARR LE 100) is an array that contains a 1 where the corresponding element of ARR is less than or equal to 100, and zero otherwise. For example, to print the number of positive elements in the array ARR:

PRINT, TOTAL(ARR GT 0)  

The following command sets B equal to ARRAY whenever the corresponding element of ARRAY is greater than or equal to 100. If the element is less than 100, the corresponding element of B is set to zero.

B = ARRAY * (ARRAY GE 100)  

Relational Operators with Infinity and NaN Values

On the Windows platform, using relational operators with the values infinity or NaN (Not a Number) causes an "illegal operand" error. The FINITE function's INFINITY and NAN keywords can be used to perform comparisons involving infinity and NaN values. For more information, see FINITE and Special Floating-Point Values.

  IDL Online Help (March 06, 2007)