It seems that JDK 8 and JDK 13 have different floating points.
I get on JDK 8, using Math:
cos(2.3) = -0.666276021279824
And on JDK 13:
cos(2.3) = -0.6662760212798241
How does this happen? Difference shows on 11th Gen Intel and on AMD Ryzen using Windows 10.
Edit 20.03.2022:
Using Long.toHexString(Double.doubleToRawLongBits()) I get different bit patterns:
I get on JDK 8:
cos(2.3) = 0xbfe5522217302fe0
And I get on JDK 13:
cos(2.3) = 0xbfe5522217302fe1
This seems to be caused by a JVM intrinsic function for Math.cos
, which is described in the related issue JDK-8242461. The behavior experienced there is not considered an issue:
The returned results reported in this bug are indeed adjacent floating-point values [this is the case here as well]
[...]
Therefore, while it is possible one or the other of the returned values is outside of the accuracy bounds, just have different return values for Math.cos is not in and of itself evidence of a problem.
For reproducible results, use the StrictMath.cos instead.
And indeed, disabling the intrinsics using -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_dcos
(as proposed in the linked issue), causes Math.cos
to have the same (expected) result as StrictMath.cos
.
So it appears the behavior you are seeing here is most likely compliant with the Math
documentation as well.