2
double check =0.615 * 255 -0.515 * 255 -0.100 * 255;
System.out.println(check);

why get -2.8421709430404007E-14? it should be 0.0

Rebecca
  • 111
  • 2
  • 12

2 Answers2

3

Double operation has some precision problem. Use BigDecimal operation instead of double than you will get expected result.

  //double check =0.615 * 255 + -0.515 * 255 + -0.100 * 255;
   BigDecimal check =
             (BigDecimal.valueOf(0.615).multiply(BigDecimal.valueOf(255)))
            .add(BigDecimal.valueOf( -0.515).multiply(BigDecimal.valueOf( 255)))
            .add(BigDecimal.valueOf( -0.100).multiply(BigDecimal.valueOf( 255)));

  System.out.println(check);

Result is : 0.000

Masudul
  • 21,823
  • 5
  • 43
  • 58
2

Debugging your math:

double check0 = 0.615 * 255;
System.out.println(check0);
double check1 = -0.515 * 255;
System.out.println(check1);
double check3 = -0.100 * 255;
System.out.println(check3);
System.out.println("Result: " + (check0 + check1 + check3));

Output:

156.825
-131.32500000000002
-25.5
Result: -2.8421709430404007E-14

Remember that:

  • multiplication has higher operator priority than addition in Java
  • you should use BigDecimal if you want control over the rounding when performing operations with floating point numbers
Mena
  • 47,782
  • 11
  • 87
  • 106
  • but check2 should be 131.325 but not 131.3250000000002 – Rebecca Sep 29 '13 at 07:14
  • @Rebecca that's exactly why you want to use `BigDecimal` instead of `double` in this case. Also check out [this](http://stackoverflow.com/questions/322749/retain-precision-with-doubles-in-java) SO page to get started on the hows and whys. – Mena Sep 29 '13 at 07:16
  • BigDecimal is the answer, thanks!!! – Rebecca Sep 29 '13 at 07:24