Note that
System.out.println("val = " + String.format("%.24f", ret));
is an unnecessary mix from printf
style formatting, string concatenation, and println
. You can use printf
in the first place:
System.out.printf("val = %.24f%n", ret);
However, there is no point in requesting 24 decimal digits, when the double
precision does not even remotely provide that many digits. When you use
System.out.println("val = " + ret);
instead, it will default to the actually available digits, which yields
val = 1.0000030588238054
for Java 8 and
val = 1.0000030588238051
for Java 11.
So the difference is only in the last digit. Or more precisely
double d1 = 1.0000030588238051, d2 = 1.0000030588238054;
System.out.println((d2 - d1) == Math.ulp(d1));
prints true
, so the distance between these two values is the smallest possible with double
. There is no other double
value in between them. The specification of pow
says:
The computed result must be within 1 ulp of the exact result.
Since the code above showed, that both results have a distance of one ulp, both results would be correct when the exact result lies between both results. Wolfram Alpha says, the exact result starts with
1.000003058823805246…
So it lies between these two results. So both results are correct according to the specification.
For easier comparison:
1.0000030588238054 JDK 8
1.000003058823805246… Wolfram Alpha
1.0000030588238051 JDK 11
String.format()
behaving differently?StrictMath.pow(…)
in the first place. Trying to get “strict consistency” with a particular implementation of the non-strict API makes no sense.