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
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
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
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:
BigDecimal
if you want control over the rounding
when performing operations with floating point numbers