Skip to main content

1.10) Choosing The Correct Variable Types


When performing mathematical operations on variables, the result of the operation will be of the same varaible type as the most complex variable used in the equation. For example, if we divide two integers, the result will be an integer. This is not always what we want.

Insert the following lines into a file called simple_maths3.pro.

[code]
print, 1 / 2
END
[/code]

Using the IDL command prompt, run the program.

.r simple_maths3

IDL will print the number "0" to the screen. This is because we have used integers for the division, so the result will be an integer.

Re-edit the program so that it looks like the lines below and then run it again.

[code]
print, 1. / 2.
END
[/code]

IDL will now print "0.5" to the screen. This is because by adding the "." after each number we have defined them as floating point numbers and the result is now also a floating point number.

It is good practice to always put a "." after a number unless you really do need an integer for some reason.