next up previous contents
Next: Arrays Up: Section 3: Some ``nuts Previous: Section 3: Some ``nuts   Contents

Variables, data types

IDL has several types of variables, many are shown in the table below. Text is kept in a ``string''. IDL can store numbers as integers (whole numbers), floats (numbers with decimal points, even if there's a zero after the point), complex numbers (with a real and imaginary parts) etc.

type range bytes to define to convert
byte 0 to 255 1 b=15B b=byte(x)
integer -32768 to +32767 2 i=15 i=fix(x)
long -2147483648 to +2147483647 4 j=long(15) j=long(x)
      j=147483647  
floating pt $\pm 10^{38}$, 7 sig figs 4 y=1.7 y=float(x)
double prec. $\pm 10^{308}$, 14 sig figs 8 y=1.7d0 d=double(x)
complex two floating point no.s 8 z=complex(1.2,0.3) z=complex(x)
string (used for text) 0-32767 s='blah' s=string(x)

Now try,

a=5
b=3
c=a/b
print,c

This probably isn't the answer you were expecting! Look at ``help,a'' ``help,b'' and ``help,c''. These are all integers. This brings us onto the subject of how IDL stores variables.

Try ,

a=5.
b=3.
print,a/b

This should give you answer you expect. Use help to look at ``a'' and ``b''. They are now floating point numbers. This is why you get a different answer from when you used integers.

If you multiply or divide two variables IDL gives the answer as the more complex type. So combining two integers gives you an integer, combining an integer and a real gives you a real.

  1. Variable names can have letters, numbers and underscores in them.

  2. They are NOT case-sensitive: foo, Foo and FOO are all the same variable.

  3. Note that the default type of integer is only a 2-byte integer. (Remember if you want a big loop index variable! BUT remember loops are BAD in IDL.

To convert one variable form to another use fix. Eg

str_var=fix(153.15,type=7) ;sets str_var to a string
help,str_var 
;STR_VAR         STRING    = '      153.150'; shows that it is a string, but it has much white space
;remove this with
str_var=strtrim(str_var,2); 2 removes white space from the front and back
(I do this so often that I have a routine called trmfix that combines fix,type=7 and strtrim).

IDL is a dynamically typed language (you don't declare all variables first like you do in Fortran).


next up previous contents
Next: Arrays Up: Section 3: Some ``nuts Previous: Section 3: Some ``nuts   Contents
John Marsham 2009-12-07