Skip to content
Advertisement

Safely convert `float` to `double` without loss of precision

I have the following head scratcher, demonstrated in jshell 11.0.12 and 17.0.1 here

JavaScript

The same thing occurs in a simple compiled class in both versions

JavaScript
JavaScript

Although 17 warns that strictfp is no longer required

JavaScript

The JLS says this in section 5.1.2

JavaScript

In 14 it contained the following after the bullet list

JavaScript

Based on my reading this is a bug in the implementation? The only reliable way I’ve found to perform this conversion is Double.parseDouble(Float.toString(13.9f)).

Advertisement

Answer

Based on my reading this is a bug in the implementation?

No. It’s a bug in your expectations. The double value you’re seeing is exactly the same value as the float value. The precise value is 13.8999996185302734375.

That’s not the same as “the closest double value to 13.9″ which is 13.9000000000000003552713678800500929355621337890625.

You’re assigning the value 13.8999996185302734375 to a double value, and then printing the string representation – which is 13.899999618530273 as that’s enough precision to completely distinguish it from other double values. If it were to print 13.9, that would be a bug, as there’s a double value that’s closer to 13.9, namely 13.9000000000000003552713678800500929355621337890625.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement