JRJC - What is a floating point number

99% of the time that I'm working with numbers, I use some form of integer such as:

    int ducks = 0 ;
    for ( int i = 0 ; i < 365 ; i++ )
    {
        ducks += numDucksSpotted( i );
    }

Integers are very fast on any computer. Plus they contain extremely accurate values and take up little memory. On the down side, they cannot handle decimal values (10.32) and they have a limited range (the int type works with values -2147483648 to 2147483647).

Floating point numbers are a little slower and take up a little bit more memory, but they do handle decimals and very extreme values.

The most common form of a floating point variable is double:


    double poundsOfChicken = 0.0 ;
    while ( poundsOfChicken < 12.5 )
    {
        poundsOfChicken += processAnotherChicken();
    }
    cookDemChickens();

Look on page 93 of Just Java 1.2 for more info on the use of double and float. In Just Java 2 (sixth edition) look on pages 49-52.






© 2000 Paul Wheaton