R
Ramon
Hi,
I had first a problem when using double primitive data type. I've tried
to add to double numbers but the result was technically incorrect. Here
is a snippet that was taken from the program:
double a = -0.7;
double b = 0.2;
System.err.println( a + b ); // outputs -0.49999999999999994 instead of -0.5
I've also tried to use BigDecimal but still no luck. Here is part of
the program:
BigDecimal a = new BigDecimal(-0.7);
BigDecimal b = new BigDecimal(0.2);
System.err.println( a.add(b) ); // unprecise output
So, my question is: Is there a way to perform an accurate addition of
-0.7 and 0.2 in Java?
Thanks.
I had first a problem when using double primitive data type. I've tried
to add to double numbers but the result was technically incorrect. Here
is a snippet that was taken from the program:
double a = -0.7;
double b = 0.2;
System.err.println( a + b ); // outputs -0.49999999999999994 instead of -0.5
I've also tried to use BigDecimal but still no luck. Here is part of
the program:
BigDecimal a = new BigDecimal(-0.7);
BigDecimal b = new BigDecimal(0.2);
System.err.println( a.add(b) ); // unprecise output
So, my question is: Is there a way to perform an accurate addition of
-0.7 and 0.2 in Java?
Thanks.