IDL can create a variety of numeric and string constants, with the data type and value depending upon the syntax.
When creating constants in your programs, you should know the data type of the constant and how it will be used in expressions. Having an incorrect data type for a constant can be inefficient and change your results.
For example, if the data type does not match the other types in an expression, then either the other variables or your constant will be converted to match. Consider the following expression:
a = findgen(100)
b = a + 5
If the variable a is of floating-point type, the constant 5 must be converted from short integer type to floating point each time the expression is evaluated. In this case it would be better to write:
a = findgen(100)
b = a + 5.0
On the other hand, imagine the array is of type byte:
a = bindgen(100)
b = a + 5
In this case the computation will be done using integer arithmetic and the result will be an array of type integer, leading to unexpected results if your algorithm expected variable b to be type byte. Here, it would be better to write:
a = bindgen(100)
b = a + 5b
Boolean Constants
There are two boolean constants: !TRUE, equal to 1, and !FALSE, equal to 0. Both of these constants are variables of type byte with the boolean flag.
Integer Constants
Numeric constants of different types are represented by the constant followed by an optional suffix specifying the type:
Type |
Suffix |
Examples |
Byte 8-bit unsigned
|
b, ub
|
12b, 34B, 34ub
|
Short 16-bit integer
|
(nothing), s
|
12, 12s, 12S
|
Unsigned short integer
|
u, us
|
12u, 12us
|
Long 32-bit integer
|
l
|
12l, 12L
|
Unsigned long integer
|
ul
|
12uL, 12UL
|
64-bit integer |
ll
|
12ll, 12LL
|
Unsigned 64-bit integer
|
ull
|
12ull, 12uLL
|
Note: The suffix is case insensitive.
Absolute values of integer constants are given in the following table.
Type |
Absolute Value Range
|
Byte |
0 – 255 |
Integer |
0 – 32767 |
Unsigned Integer
|
0 – 65535 |
Long |
0 – 231–1
|
Unsigned Long |
0 – 232–1
|
64-bit Long |
0 – 263–1
|
Unsigned 64-bit Long
|
0 – 264–1
|
Integers specified without one of the B, S, L, or LL specifiers are automatically promoted to an integer type capable of holding them. For example, 40000 is promoted to longword because it is too large to fit in an integer. Any numeric constant can be preceded by a plus (+) or minus (-) sign. The following table illustrates examples of both valid and invalid IDL constants.
Unacceptable |
Reason |
Acceptable |
256B |
Too large, limit is 255
|
255B |
'123L |
Missing apostrophe
|
'123'L |
0x300G |
Invalid character
|
0x300F |
'27'L |
No radix |
'27'oL |
"129 |
9 is an invalid octal digit
|
"127 |
Hexadecimal Constants
The syntax used when creating integer constants is shown in the following table, where N represents one or more digits and <T> represents the optional type suffix from the table above.
Form |
Examples |
Notes |
0xNNNN<T>
|
0xA9, 0xFA62, 0xFFFFFFuLL |
Recommended form
|
'NNNN'x<T>
|
'A9'x, 'FA62'x, 'FFFFFFF'xull
|
Deprecated |
Note: To create a hexadecimal constant of type byte, be sure to use ub for the suffix, not b. Otherwise the b may be treated as part of your hex number. For example, a = 0x5Bub.
Octal Constants
The syntax used when creating integer octal constants is shown in the following table, where N represents one or more digits and <T> represents the optional type suffix from the table above.
Form |
Examples |
Notes |
0oNNNN<T>
|
0o377, 0o777777UL |
Recommended form |
'NNNN'o<T>
|
'377'o, '777777'oUL |
Deprecated |
"NNNN<T>
|
"12, "12ub |
Deprecated |
Binary Constants
The syntax used when creating integer binary constants is shown in the following table, where N represents one or more digits and <T> represents the optional type suffix from the table above.
Form |
Examples |
Notes |
0bNNNN<T>
|
0b1010, 0b10101011111uLL
|
Recommended form |
'NNNN'b<T>
|
'1010'b, '10101011111'bull
|
Deprecated |
Short Integers and the DEFINT32 Compile Option
When creating short 16-bit integers (signed or unsigned), you can omit the "s" if the DEFINT32 (or IDL2) compile option is not turned on. If the DEFINT32 (or IDL2) compile option is turned on, then by default any integer constants will be 32-bits and you need to use the "s" or "us" to force IDL to create 16-bit integers. For example:
IDL> help, 1234, 1234u
<Expression> INT = 1234
<Expression> UINT = 1234
IDL> compile_opt DEFINT32
IDL> help, 1234, 1234u
<Expression> LONG = 1234
<Expression> ULONG = 1234
IDL> help, 1234s, 1234us
<Expression> INT = 1234
<Expression> UINT = 1234
Floating-Point and Double-Precision Constants
Floating-point and double-precision constants can be expressed in either conventional or scientific notation. Any numeric constant that includes a decimal point is a floating-point or double-precision constant.
The syntax of floating-point and double-precision constants is shown in the following table. The notation “sx” represents the sign and magnitude of the exponent, for example, E-2.
Form |
Single-Precision Example
|
Double-Precision Example
|
n. |
102.
|
102d
|
.n |
.102
|
.102d
|
n.n
|
10.2
|
10.2d
|
nE
|
10e
|
10d
|
nEsx
|
10e5
|
10d5
|
n.Esx
|
10.e-3
|
10.d-3
|
.nEsx
|
.1e+12
|
.1d+12
|
n.nEsx
|
2.3e12
|
2.3d12
|
Double-precision constants are entered in the same manner, replacing the E with a D. For example, 1.0D0, 1D, and 1.D each represent a double-precision numeral 1.
Note: The nE and nD forms are shorthand for nE0 and nD0, and are usually used to indicate the type of the number, either single or double precision. When using these forms in expressions, be sure to leave a space after the E or D if the next term has a + or - sign.
For example, the expression 1D+45 is evaluated as 1x1045 in double precision, while 1D + 45 (note the spaces) evaluates to the number 46 in double precision. Similarly, the expression 1D+x gives an error, because there was no space after the D. The correct way to write this expression is 1D + x (note the spaces).
Floating-point Values and the FLOAT64 Compile Option
By default, floating-point numbers without the "d" type specifier will be type FLOAT (32 bits). If the FLOAT64 (or IDL3) compile option is turned on, then by default any floating-point constants will be type DOUBLE (64 bits). You can always use the "e" (exponent) type specifier to force the constant to be type FLOAT. For example:
IDL> help, 2459810.8580208337, /full
<Expression> FLOAT = 2459810.8
IDL> compile_opt float64
IDL> help, 2459810.8580208337, /full
<Expression> DOUBLE = 2459810.8580208337
IDL> help, 2459810.8580208337e, /full
<Expression> FLOAT = 2459810.8
Complex Constants
Complex constants contain a real and an imaginary part, both of which are single- or double-precision floating-point numbers. You can create complex numbers by using "i" or "j" to indicate the complex part, along with an optional "d" to indicate double-precision complex:
IDL> HELP, 1 + 2i, 1 + 2j, 0.5i, 0.5di, 1e5j, 1d5j
<Expression> COMPLEX = ( 1.00000, 2.00000)
<Expression> COMPLEX = ( 1.00000, 2.00000)
<Expression> COMPLEX = ( 0.00000, 0.500000)
<Expression> DCOMPLEX = ( 0.0000000, 0.50000000)
<Expression> COMPLEX = ( 0.00000, 100000.)
<Expression> DCOMPLEX = ( 0.0000000, 100000.00)
There is no difference between using "i" or "j", and you can also use capital I or J as well. To create complex numbers with a zero imaginary component, simply use 0i:
IDL> HELP, 1.234 + 0i
<Expression> COMPLEX = ( 1.23400, 0.00000)
Instead of using the "i" notation, you can use the COMPLEX or DCOMPLEX functions:
COMPLEX(REAL_PART, IMAGINARY_PART)
DCOMPLEX(REAL_PART, IMAGINARY_PART)
You can omit the IMAGINARY_PART, in which case it will be set to 0.
Tip: Because of the overhead of making a function call, it is much faster to use the "i" notation when creating complex numbers, even if your imaginary part is zero.
Use the REAL_PART function to extract the real part of a complex number, and IMAGINARY to extract the imaginary part. The ABS function returns the magnitude.
Complex Values and the FLOAT64 Compile Option
By default, floating-point numbers without the "d" type specifier will be type FLOAT (32 bits). You should be aware of this when creating double-precision complex numbers, and be sure to use the "d" type specifier. For example:
IDL> help, 2459810.8580208337 + 123456789.12345678i, /full
<Expression> COMPLEX = ( 2459810.8, 1.2345679e+08)
IDL> help, 2459810.8580208337d + 123456789.12345678di, /full
<Expression> DCOMPLEX = ( 2459810.8580208337, 123456789.12345678)
If the FLOAT64 (or IDL3) compile option is turned on, then by default any floating-point constants will be type DOUBLE (64 bits). For example:
IDL> compile_opt float64
IDL> help, 2459810.8580208337 + 123456789.12345678i, /full
<Expression> DCOMPLEX = ( 2459810.8580208337, 123456789.12345678)
String Constants
A string constant consists of zero or more characters enclosed by apostrophes (') or quotes (“). The value of the constant is the characters appearing between the leading delimiter ('or “”) and the next occurrence of the same delimiter. A double apostrophe ('') or quote (“”) is considered to be an empty string; a string containing no characters. An apostrophe or quote can be represented within a string by two apostrophes or quotes; e.g., 'Don''t' returns Don't. This syntax often can be avoided by using a different delimiter; e.g., “Don't” instead of 'Don''t'. The following table illustrates valid string constants.
Expression |
Resulting String
|
'Hi there' |
Hi there |
"Hi there" |
Hi there |
'' |
Empty String
|
"I'm happy" |
I'm happy |
'I''m happy' |
I'm happy |
'counter' |
counter |
"127" |
127 |
The following table illustrates invalid string constants.
Unacceptable |
Reason |
Acceptable |
'Hi there" |
Mismatched delimiters
|
"Hi there"
|
' |
Missing delimiter
|
'' or "" |
'I'm happy' |
Apostrophe in string
|
'I''m happy' or "I'm happy"
|
"127 |
Missing quote, will be treated as octal
|
"127" |
Note: String constants that start with a double-quote character and a number may be incorrectly interpreted as an octal constant. If the number is followed by an operator (+-*/^?:,)) then this will always be treated as an octal constant, even if there is a trailing double-quote character. To avoid ambiguity, you should always use single-quote characters with strings that begin with a number.
Note: See String Operations for details on working with strings.
Template Literal Strings
In IDL, you can create string constants using single or double quotes. You can also create template literal strings using backtick characters. These template literal strings can contain IDL expressions (the "templates") and can span multiple lines, with all whitespace and line breaks preserved in the final string (hence the word "literal"). At runtime, IDL evaluates all of the expressions within the template literal string, converts each expression to a string, and then concatenates all of the strings to produce a scalar IDL string constant.
For example:
a = [1, 2, 3, 4, 5]
result = `There are ${total(a gt 2, /int)} matches\nin the array ${a}.`
print, result
IDL prints:
There are 3 matches
in the array [1,2,3,4,5].
For more details see Template Literal Strings.
Version History
Original |
Introduced |
8.6.1 |
Added 0x form for hexadecimal constants.
|
8.9 |
Added 0o for octal constants, 0b for binary constants.
Added FLOAT64 compile option.
Added "i" and "j" to create complex numbers.
Added template literal strings.
|