Question

Please review the Oracle Specification - Chapter 5.

This line:

A widening primitive conversion does not lose information about the overall magnitude of a numeric value.

is followed , just 2 lines below, by this line which says that magnitude information can be lost.

A widening primitive conversion from float to double that is not strictfp may lose information about the overall magnitude of the converted value.

This seems like an obvious contradiction; is this a mistake?

Was it helpful?

Solution 2

For what it's worth, the JVM specification is a little bit clearer in the wording in 2.11.4:

Widening numeric conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from int to long and int to double do not lose any information at all; the numeric value is preserved exactly. Conversions widening from float to double that are FP-strict (§2.8.2) also preserve the numeric value exactly; however, such conversions that are not FP-strict may lose information about the overall magnitude of the converted value.

OTHER TIPS

Yes, it does. From the Oracle:

Hi Bill,

Thanks for your mail. I agree there is a contradiction between:

"A widening primitive conversion does not lose information about the overall magnitude of a numeric value."

and:

"A widening primitive conversion from float to double that is not strictfp may lose information about the overall magnitude of the converted value."

Only the first sentence appeared in the First Edition of the JLS, prior to the introduction of strictfp. The second sentence appeared in the Second Edition of the JLS, as part of strictfp support, and the first sentence should have been modified at the same time. In the Java SE 8 Edition, I will clarify that only some widening primitive conversions do not lose magnitude information.

It's more of a clarification. In the specific case of a float to double conversion, there is no guarantee of not losing information unless strictfp is designated

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top