...
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 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 2005] 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.
...
FLP36-C. Preserve precision when converting integral values to floating-point type | |
VOID FLP36-CPP. Beware of precision loss when converting integral types to floating point |
Bibliography
...