Sub topic 13
Overflow and Floating-Point Precision
Java numbers have limits. A result can be too large to fit in its type, or it can be slightly different from the value you expect. Knowing about these limits helps you avoid errors in calculations.
Integer overflow
An integer is a whole number, such as 3 or -10. Java’s int type can hold values from -2147483648 to 2147483647.
Overflow happens when an integer calculation produces a value outside the type’s range. With ordinary int addition, Java does not report an error. Instead, the result wraps around from one end of the range to the other.
For example, adding 1 to the largest int gives -2147483648, the smallest int.
A long can hold larger whole numbers. But changing the result to long after an int calculation is too late: the overflow has already happened. Make at least one value a long before the calculation. You can do this by writing 1L instead of 1.
A long also has a limit, so it can overflow too.
Floating-point precision
A floating-point type stores numbers that can have fractional parts. Java’s float and double are floating-point types. A double can usually store a value more precisely than a float, but neither can represent every decimal value exactly.
Precision describes how closely a stored value can match a number. Java stores floating-point values in binary, a number system that uses 0 and 1. Some simple decimal fractions, including 0.1, cannot be stored exactly this way.
As a result, 0.1 + 0.2 produces a value slightly above 0.3. Comparing the result directly with 0.3 gives false.
Try both problems
Save this program as NumberLimits.java:
import java.math.BigDecimal;
class NumberLimits {
public static void main(String[] args) {
int limit = 2147483647;
System.out.println(limit + 1);
long tooLate = limit + 1;
System.out.println(tooLate);
long safeHere = limit + 1L;
System.out.println(safeHere);
double total = 0.1 + 0.2;
System.out.println(total);
System.out.println(total == 0.3);
System.out.println(Math.abs(total - 0.3) < 0.0000001);
BigDecimal money = new BigDecimal("0.10")
.add(new BigDecimal("0.20"));
System.out.println(money);
double huge = 1e308;
System.out.println(huge * 10);
}
}Compile and run it:
javac NumberLimits.java
java NumberLimitsThe output is:
-2147483648
-2147483648
2147483648
0.30000000000000004
false
true
0.30
InfinityThe first two lines show overflow. The third uses 1L, so the addition happens as a long calculation.
The comparison using Math.abs checks the size of the difference between the floating-point values. It treats them as close when that difference is less than 0.0000001. Choose a suitable limit for the values in your own calculation.
When exact decimal values matter
BigDecimal is a Java type for decimal calculations. The example creates its values from text, such as "0.10", so those starting values are exact. It is useful for money, where small rounding differences matter. Some calculations, such as division, may still need a rounding rule.
Floating-point calculations can also overflow. The last line shows Infinity, a special value produced here because the result is too large for a finite double.
Exercise
If int n = 2147483647, what does n + 1L produce, and why is it different from n + 1?
Show answer
It produces 2147483648 as a long. The 1L makes the addition use long values. With n + 1, the addition uses int values and overflows.