1

Possible Duplicate:
Java floating point arithmetic

Hi All, I was just reading a book and i came across this example which gave different outputs. Can somebody explain to me why.

 public class Test {

    public static void main(String[] args) {
        double x = 0.3 - 0.2;
        double y = 0.2 - 0.1;
        System.out.println(x);
        System.out.println(y);

        System.out.println(y == x);
    }
}

Output is : 0.09999999999999998

0.1

false

Community
  • 1
  • 1
Abhishek
  • 1,031
  • 4
  • 16
  • 25
  • 1
    Take a look at http://www.ecs.umass.edu/ece/koren/arith/simulator/FPAdd/ to see how calculation is performed with IEEE 754 standard (dec/sub/double and click compute). Use BigDecimal class if you need exact values (but it's slower). – Grzegorz Szpetkowski May 22 '11 at 12:48

2 Answers2

0

Computers represent floating-point numbers as an integer times a power of 2. 0.1 is not exactly representable as a floating point number (in the same way you can't write 1/3 in decimal) and you therefore get rounding errors. The best way to avoid this is to use integer types when you can and to use a tolerant comparison function when you can't. Do not compare floating point numbers for exact equality unless you know what you are doing.

sverre
  • 6,768
  • 2
  • 27
  • 35
0

This example demonstrates that floating point arithmetics in computers isn't precise. Variable value actually depends on what number is stored in variable. Some have bigger error some less, but either way you can't assume that value is exact as in mathematic rules.

Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96