-1

Possible Duplicate:
Retain precision with Doubles in java
Floating point inaccuracy examples

I know that there is a good reason pertaining to objects and the way doubles are stored in Java that makes this if statement false. My understanding is for some reason, it's really 0.29999999 instead of .3. Could some one explain to me further and intuitively why this conditional returns false?

public class hello {
    public static void main(String[] args) {

        double a = 0.1;
        double b = 0.1;
        double c = 0.3;

        if( a+b+a == c )
            System.out.println("if is true");
    }
}
Community
  • 1
  • 1
mathjacks
  • 335
  • 2
  • 4
  • 11

2 Answers2

1

You can use BigDecimal to see what the actual values are.

double a = 0.1;
double c = 0.3;

System.out.println(a + " is actually " + new BigDecimal(a));
System.out.println(c + " is actually " + new BigDecimal(c));
double a3 = a * 3;
System.out.println("0.1 * 3 or " + a3 + " is actually " + new BigDecimal(a3));
double ac = a + c;
System.out.println("0.1 + 0.3 or " + ac + " is actually " + new BigDecimal(ac));

prints

0.1 is actually 0.1000000000000000055511151231257827021181583404541015625
0.3 is actually 0.299999999999999988897769753748434595763683319091796875
0.1 * 3 or 0.30000000000000004 is actually 0.3000000000000000444089209850062616169452667236328125
0.1 + 0.3 or 0.4 is actually 0.40000000000000002220446049250313080847263336181640625

As you can see, double cannot represent 0.1 precisely. When you print the value for 0.1 it does a small amount of rounding which hides the representation error. This is looks fine except when you perform an un-rounded calculation and you can see the representation error has accumulated.

You can see that 0.1 * 3 results in an error which is large enough to be noticeable. However 0.1 + 0.3 works out to be the same as 0.4 (This is because the error for 0.1 is slightly too large and for 0.3 its slightly too small)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Java (i think other programming languages as well) doesn't represent float and double with accurate precision. Read this article Comparing floating point numbers. In Java, you are expected to use BigDecimal.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327