...
Conversion from int
or long
to float
or from long
to double
can lead to loss of precision (loss of least significant bits). In these cases, the resulting floating-point value is a rounded version of the integer value, using IEEE 754 round-to-nearest mode. Despite this loss of precision, The Java Language Specification (JLS) requires that the conversion and rounding occur silently, that is, without any runtime exception (see the JLS, §5.1.2, "Widening Primitive Conversion" [JLS 20052015], for more information). Conversions from integral types smaller than int
to a floating-point type and conversions from int
to double
can never result in a loss of precision. Consequently, programs must ensure that conversions from an int
or long
to a floating-point type or from long
to double
do not result in a loss of required precision.
...
Bibliography
[Seacord 2015] | NUM13-J. Avoid loss of precision when converting primitive integers to floating-point LiveLesson |
...