Precision of Double

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

What is wrong with this float?

1230.00089F

Answer:

There are nine decimal places of precision. Data type float can't handle that. (The compiler will round the number into a value that can fit in a float).

Precision of Double

You might wish to argue that there are only five places used in the above number: the places used by the digits 1, 2, 3, 8, and 9. However, the four 0's in the middle do count. It takes bits to represent them, even if they are zeros.

Primitive data type double uses 64 bits, and has a much greater range, -1.7E+308 to +1.7E+308. It also has a much greater precision: about 15 significant decimal digits.

Because of this, if you write a literal like 2.345 in a Java program, it will automatically be regarded as a double, even though a float might be good enough. The other numbers in the program might need to be double, so we might as well make them all double.

QUESTION 12:

Do you suspect that characters are important enough to be one of the eight primitive data types?