19. Restoring Always-Strict Floating-Point semantics
Floating-point calculations are not easy! Even some simple arithmetical properties don’t apply to such calculations. For instance, floating-point addition or multiplication is not associative. In other words (x + y) + z is not equal to x + (y + z) where x, y, and z are real numbers. A quick example to test the associativity of multiplication follows:
double x = 0.8793331;
double y = 12.22933;
double z = 901.98334884433;
double m1 = (x * y) * z; // 9699.617442382583
double m2 = (x * (y * z)); // 9699.617442382581
// m1 == m2 returns false
This means that floating-point arithmetic is a methodical approximation of real arithmetic. Computers have to approximate because of some limitations. For instance, exact floating-point outputs become very large quite quickly. Moreover, the exact inputs are not known, so with inexact inputs, it is difficult to obtain exact outputs.
To solve this problem, Java has to adopt...