Video lecture
Watch on YouTube(opens in a new tab)Sub topic 5
Floating-Point Numbers: float and double
Whole-number types are useful for counts, but many measurements have a fractional part. A temperature might be 20.5 degrees, or a distance might be 19.99 meters. Java provides two primitive types for values like these: float and double.
A primitive type is a basic Java type that holds a value directly. A floating-point number is a way to store a number with a fractional part and a wide range of sizes. The name comes from the way the position of the point can change, allowing the same type to represent both small and large numbers.
Floating-point numbers are useful, but they have a limit: some decimal values can be stored only approximately.
The two floating-point types
| Type | Size of its value | Approximate decimal precision | Largest positive finite value |
|---|---|---|---|
| `float` | 32 bits | 6–7 significant digits | 3.4 × 10^38 |
| `double` | 64 bits | 15–16 significant digits | 1.8 × 10^308 |
A bit is a unit of stored information that can be either 0 or 1. The sizes in the table describe the values themselves, not necessarily all the memory a program uses to hold them.
Precision describes how much detail a type can keep in a number. Significant digits are the meaningful digits in a number, not just the digits after the decimal point. For example, 123.45 has five significant digits.
The precision counts in the table are rough guides. They do not mean that every decimal value with that many digits is stored exactly.
Both types can also hold negative values and zero. Their largest finite values show their range: how large or small their values can be. Range and precision are different. A type can hold a very large number while still losing some of its detail.
Writing floating-point values
A literal is a value written directly in Java code. Here is a double variable with a floating-point literal:
double distance = 19.99;A variable is a named place for a value. Its declared type, here double, tells Java what kind of value it holds.
A decimal literal such as 19.99 is a double by default. To write a float literal, add f or F to the end:
float temperature = 20.5f;You can add d or D to a double literal, but you do not need to:
double height = 1.75d;The suffix matters. The following line does not compile because 20.5 is a double literal, while the variable is a float:
float temperature = 20.5; // Error: the literal is a doubleTo write the value as a float literal, use 20.5f.
Very large and very small values
Java also accepts scientific notation, a short way to write a number multiplied by a power of ten. For example, 1.2e3 means 1.2 multiplied by 1,000, or 1200.0. A negative power moves in the other direction: 2.5e-3 means 0.0025.
double largeDistance = 1.2e3;
float smallDistance = 2.5e-3f;The f still matters for the float literal. Scientific notation changes how you write the value; it does not give the type extra precision.
A worked example
Save this program in a file named FloatingPointExample.java:
class FloatingPointExample {
public static void main(String[] args) {
float temperature = 20.5f;
float increase = 1.5f;
double distance = 19.99;
System.out.println(temperature);
System.out.println(temperature + increase);
System.out.println(distance);
double total = 0.1 + 0.2;
System.out.println(total);
System.out.println(total == 0.3);
}
}Compile and run it from the folder containing the file:
javac FloatingPointExample.java
java FloatingPointExampleThe output is:
20.5
22.0
19.99
0.30000000000000004
falseThe first two variables are float values. Their literals have the f suffix. The distance variable is a double, so its literal needs no suffix. The program prints the temperature, the result of adding the increase, and the distance.
The last two lines need more explanation. The == symbol checks whether two values are equal. In ordinary decimal arithmetic, 0.1 plus 0.2 is 0.3. But the program prints a slightly different result and then prints false. This happens because floating-point values cannot store every decimal fraction exactly.
Why some values are approximate
Java stores float and double values using binary, a number system based on 0 and 1. In contrast, everyday decimal numbers use ten digits, from 0 through 9.
Some fractions fit exactly in both systems. For example, 0.5 can be stored exactly as a float or a double. Other fractions, including 0.1, do not have an exact finite floating-point representation. Java stores a nearby value instead.
The displayed text can hide the small difference. Printing 0.1 normally shows 0.1, even though the stored value is not exactly one tenth. When arithmetic combines values, a small difference may become visible, as it did in the worked example.
double keeps more detail than float, but it does not make every decimal fraction exact. Choosing double can reduce some errors; it does not remove this basic limit.
Choosing between float and double
For most beginner programs involving measurements or general fractional values, start with double. It offers more precision and is the default type for decimal literals.
Use float when you have a reason to use its smaller 32-bit value. For example, a library or a file format may require float values, or a program may need to store many of them. A library is a collection of code that a program can use.
Do not choose float just because a number has only a few digits after the decimal point. The question is how much detail the calculation needs, not just how short the starting value looks.
For money, take special care. Currency calculations often need exact decimal amounts and clear rounding rules. Java provides BigDecimal for decimal arithmetic when that control is needed. Using double for a measurement is often reasonable; using it for exact currency calculations can produce surprising results.
Check your understanding
Exercise
Write a declaration for a float variable named rainfall with the value 12.5. Why does the literal need a suffix?
Show answer
Write float rainfall = 12.5f;. The f makes the literal a float. Without it, 12.5 is a double literal.
Exercise
Which type would you usually start with for a distance measurement: float or double? Why?
Show answer
Start with double in most cases. It provides more precision, and decimal literals are double values by default. Use float when a requirement or storage need calls for it.
Exercise
A program prints 0.1 and later finds that 0.1 + 0.2 == 0.3 is false. What explains this?
Show answer
Floating-point values use binary storage. The decimal fractions are not all stored exactly, so their sum can be slightly different from the stored value of 0.3. Printing 0.1 can hide its small difference.