Sunday, July 15, 2018

java - Floating point arithmetic not producing exact results




I need to do some floating point arithmetic in Java as shown in the code below:




public class TestMain {
private static Map ccc = new HashMap() {
{ put(1, 0.01); put(2, 0.02); put(3, 0.05); put(4, 0.1); put(6, 0.2);
put(10, 0.5); put(20, 1.0); put(30, 2.0); put(50, 5.0); put(100, 10.0);
}
};

Double increment(Double i, boolean up) {
Double inc = null;


while (inc == null) {
inc = ccc.get(i.intValue());

if (up)
--i;
else
++i;
}
return inc;

}

public static void main(String[] args) {
TestMain tt = new TestMain();

for (double i = 1; i < 1000; i += tt.increment(i, true)) {
System.out.print(i + ",");
}
}
}



This is to simulate the range of values given as output by the Betfair spinner widget.



Floating point arithmetic in Java seems to introduce some unexpected errors. For example, I get 2.180000000000001 instead of 2.18. What use are floating point numbers is you can't trust the results of arithmetic performed on them? How can I get around this issue?


Answer



If you need exact decimal values, you should use java.math.BigDecimal. Then read "What Every Computer Scientist Should Know About Floating-Point Arithmetic" for the background of why you're getting those results.



(I have a .NET-centric article which you may find easier to read - and certainly shorter. The differences between Java and .NET are mostly irrelevant for the purposes of understanding this issue.)


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...