What is the difference between a float variable and an interger variable?

SNAP PAC Learning

What is the difference between a float variable and an interger variable?


kdowney@opto22.com 06-29-2007, 2:37 PM

An integer variable is a whole number, i.e. 400, 0, or -3.  A float variable is a number with a decimal, i.e. 3.14 or 98.6.  Digital inputs and outputs are usually stored in integers while analog inputs and outputs are usually stored in float variables.  You can store a decimal in an integer variable just know that the number will be rounded to the closest integer.  Likewise, you can store an interger in a float variable and the number will have zero after the decimal point.    

However, if you have a large integer number (such as 123,456,789) and move it to a float variable, the number will be stored as 1.234568e+008.  It is stored this way because of the limitations of storing a float value in a 32-bit memory location.  When you move this float variable back to an integer variable, the number may be incorrect because of the rounding that happened during the move from the original integer to the float.  For example, when I move 1.234568e+008 to an integer variable, the integer value is 123,456,792. 

Powered by Community Server, by Telligent Systems