...
Conversion from int
or long
to float
, or from long
to double
can lead to loss of precision (loss of least significant bits). In this case, 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" 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.
Note that conversions from float
to double
can also lose information about the overall magnitude of the converted value. See rule " NUM06-J . Use the strictfp modifier for floating-point calculation consistency across platforms" for additional information.
Noncompliant Code Example
...
This method could have unexpected results because of the loss of precision. Values of type float
have 23 mantissa bits, a sign bit, and an 8-bit exponent. The exponent allows type float
to represent a larger range than that of type int
. However, the 23-bit mantissa means that float
supports exact representation only of integers whose representation fits within 23 bits; float
supports only approximate representation of integers outside that range.
...
Code Block | ||
---|---|---|
| ||
class WideSample { public static int subFloatFromInt(int op1, float op2) throws ArithmeticException { // The significand can store at most 23 bits if ((op1 > 0x007fffff) || (op1 < -0x800000)) { throw new ArithmeticException("Insufficient precision"); } return op1 - (int)op2; } public static void main(String[] args) { int result = subFloatFromInt(1234567890, 1234567890); System.out.println(result); } } |
In this example, the subFloatFromInt()
method throws java.lang.ArithmeticException
. This general approach, with appropriate range checks, should be used for conversions from long
to either float
or double
.
...
This compliant solution accepts an argument of type double
instead of an argument of type float
. Values of type double
have 52 mantissa bits, a sign bit, and an 11-bit exponent. Integer values of type int
and narrower can be converted to double
without a loss of precision.
...
NUM13-EX0: Conversion from integral types to floating-point types need no without a range check if is permitted when suitable numerical analysis demonstrates that the loss of the least significant bits of precision is acceptable.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d14180153fb76e59-47f7294f-4e8048e3-a9bb892e-a9048d7d094b34b780204fb2"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [§5.1.2, " Widening Primitive Conversion " | http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2] | ]]></ac:plain-text-body></ac:structured-macro> |
...