Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are results from math operations on Double exactly repeatable?

I have a complex data processing algorithm implemented using Java 8 and using Double as data type. Given the same input (many hundred thousand database rows) this algorithm outputs different Double values. Sometimes value a is returned, sometimes value b. These two values alternate each execution. The difference between a and b is ~0.0001. I am aware that Double datatype does not grant exact values like Decimal does. I am not sure, however, if it grants repeatable results, assuming exactly same input. I.e. would it be possible that rounding policies are applied in a non-consistent way? My purpose here would be to explain why I get different values with same input.

Other details: I am using Tomcat 8 runtime environment, deployment done on SAP HCP. Datatype at database level is Decimal, we need Double at Java level for historical reasons.

like image 370
MoZZoZoZZo Avatar asked Nov 28 '22 06:11

MoZZoZoZZo


1 Answers

Given the same input, floating point number calculations (on the same platform) will result in the same output.

However, you may get different results if you feed in the input numbers in different orders (even though that would not make a difference mathematically). Maybe that is what is happening here (seeing that you pull the numbers out of a relational database, which has no defined order unless you explicitly sort). Same thing can happen if you have parallel computations using multiple CPU and the data is divided and combined differently each time.

like image 146
Thilo Avatar answered Dec 09 '22 11:12

Thilo