Precision of Floating Point Numbers

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 the following number if written out the usual way: 1.9345E+03

Answer:

1934.5

Precision of Floating Point Numbers

Consider writing the value 1/3 in decimal notation:

0.333333333333333333

There is no limit to the number of 3's required for complete accuracy. With a limited amount of paper, you can not be completely accurate. With any data type, there is a limited number of bits. They cannot accurately represent an unlimited number of 3's.

The data type float has 23 bits of precision. This is equivalent to only about 7 decimal places. (The rest of the 32 bits are used for the sign and size of the number.)

The number of places of precision for float is the same no matter what the size of the number. Data type float can represent numbers as big as about 3.4E+38. But the precision of these large numbers will also be about 7 decimal digits.

Remember: data type float has about the range and precision of a cheap electronic calculator. This is usually not sufficient.

QUESTION 11:

What is wrong with the following constant, expected to be of type float?

1230.00089F