Is the result of mathematical operation on Java – double completely repeatable?
I have a complex data processing algorithm, using java 8 and double as the data type Given the same input (hundreds of thousands of database rows), the algorithm outputs different double values Sometimes it returns the value a, sometimes it returns the value B. these two values execute each execution alternately The difference between a and B is ~ 0.0001 I know that the double data type does not grant exact values like decimal However, I'm not sure if it will give repeatable results, assuming that the inputs are exactly the same That is, is it possible to apply the rounding policy in an inconsistent manner? My goal is to explain why I get different values with the same input
Other details: I am using Tomcat 8 runtime environment to complete the deployment on SAP HCP The database level data type is decimal. For historical reasons, we need a Java level double
Solution
Orders are important If the order of database rows processed each time is different, it may produce different results This is a simple example:
double d1 = 0.1; double d2 = 0.2; System.out.println(d1 + d1 + d2 + d2); System.out.println(d1 + d2 + d2 + d1);