Puzzlers 1
Puzzler # 1.1
What is output of this code?
Puzzler1.java
public class Puzzler1 {
public static void main(String[] args) {
System.out.println(2.00 - 1.90)
}
}
Answer
The output of this code will be 0.09999999999999998
This might seem surprising since 2.00 - 1.90 should be equal to 0.10, but this is class example of floating-point arithmetic imprecsion. Here’s why this happens:
- In computers, sstems store floating-point numbers in binary (base - 2) format, but they represent decimals like 1.90 in base 10.
- Many decimal numbers cannot be exactly represented in binary floating-point format. It’s like how 1/3 cannot be exactly represented in decimal format.
- When you convert 1.90 to binary floating-point, it is stored as very close approximation, but not exactly 1.90
- This small imprecision cause the result to differ from 0.10 by small amount.
If you need exact decimal arithmetic in Java, you should use the BigDecimal class instead:
Puzzler1.java
import java.math.BigDecimal;
public class Puzzler1 {
public static void main(String[] args) {
BigDecimal a = new BigDecimal("2.00");
BigDecimal b = new BigDecimal("1.90");
System.out.println(a.subtract(b)); // Outputs: 0.10
}
}