Data Type and Structure of Expressions

Every entity in IDL has an associated data type and structure. The structure of an expression determines whether the expression can represent a single value or multiple values. IDL expressions can be either scalars (with exactly one value) or arrays (with one or more values). The data type and structure of an expression depend on the data type and structure of its operands.

Tip
You can determine the data type of an expression by returning the type code of the expression. See Returning Type and Size Information (Using IDL) for more information.

Hierarchy of IDL Data Types

Unlike many other languages, the data type and structure of most expressions in IDL cannot be determined until the expression is evaluated. Because of this, care must be taken when writing programs. For example, a variable can be a scalar byte variable at one point in a program while at a later point the same variable can hold a complex array. See Expression Type for information on how the hierarchy of data types affect the outcome of mathematical operations. See Expression Structure for information on how the results of scalar and array operations are evaluated. The twelve atomic data types in decreasing order of precedence are as follows:

Double-precision complex floating-point

Complex floating-point

Double-precision floating-point

Floating-point

Signed and unsigned 64-bit integer

Signed and unsigned longword (32-bit) integer

Signed and unsigned (16-bit) integer

Byte

String

Expression Type

IDL attempts to evaluate expressions containing operands of different data types in the most accurate manner possible. The result of an operation becomes the same data type as the operand with the greatest precedence or potential precision. For example, when adding a byte variable to a floating-point variable, the byte variable is first converted to floating-point, then added to the floating-point variable, yielding a floating-point result. When adding a double-precision variable to a complex variable, the result is double-precision complex, because the double-precision complex type has a higher position in the hierarchy of data types. See Hierarchy of IDL Data Types for the order of precedence.

Note
Signed and unsigned integers of a given width have the same precedence. In an expression involving a combination of such types, the result is given the type of the leftmost operand.

When writing expressions with mixed data types, care must be taken to obtain the desired results. For example, assume the variable A is an integer variable with a value of 5. The following expressions yield the indicated results:

; Integer division is performed. The remainder is discarded. 
A / 2 = 2 
 
; The value of A is first converted to floating. 
A / 2. = 2.5 
 
; Integer division is done first because of operator precedence.  
; Result is floating point. 
A / 2 + 1. = 3. 
 
; Division is done in floating, then the 1 is converted to floating  
; and added. 
A / 2. + 1 = 3.5 
 
; Signed and unsigned integer operands have the same precedence, so  
; the left-most operand determines the type of the result as signed  
; integer. 
A + 5U = 10 
 
; As above, the left-most operand determines the result type  
; between types with the same precedence 
5U + A = 10U 

Note
When other data types are converted to complex type, the real part of the result is obtained from the original value and the imaginary part is set to zero.

When a string type appears as an operand with a numeric data type, the string is converted to the type of the numeric term. For example: '123' + 123.0 is 246.0, while '123.333' + 33 gives the result 156 because 123.333 is first converted to integer type. In the same manner, 'ABC' + 123 also causes a conversion error.

Expression Structure

IDL expressions can contain operands that are either scalars or arrays, just as they can contain operands with different types. Conversion of variables between the scalar and array forms is independent of data type conversion. An expression will yield an array result if any of its operands is an array, as shown in the following table:

Table 13-2: Structure of Expressions

Operands
Result

Scalar : Scalar

Scalar

Array : Array

Array

Scalar : Array

Array

Array : Scalar

Array

See Operations on Array Expressions for more information on working with arrays as operands in an expression.