...
Note that conversions from float
to double
can also lose information about the overall magnitude of the converted value. See rule NUM06 NUM53-J. Use the strictfp modifier for floating-point calculation consistency across platforms for additional information.
...
This method could have unexpected results because of the loss of precision. In FP-strict mode, values of type float
have 23 mantissa bits, a sign bit, and an 8-bit exponent. See rule NUM06 NUM53-J. Use the strictfp modifier for floating-point calculation consistency across platforms for more information about FP-strict mode. 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 | ||
---|---|---|
| ||
strictfp class WideSample {
public static int subFloatFromInt(int op1, float op2) {
return op1 - (int)op2;
}
public static void main(String[] args) {
int result = subFloatFromInt(1234567890, 1234567890);
// This prints -46, and not 0 as may be expected
System.out.println(result);
}
}
|
...
This compliant solution range checks the argument of the integer argument (op1
) to ensure it can be represented as a value of type float
without a loss of precision.
Code Block | ||
---|---|---|
| ||
strictfp class WideSample {
public static int subFloatFromInt(int op1, float op2)
throws ArithmeticException {
// The significand can store at most 23 bits
if ((op2 > 0x007fffff) || (op2 < -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);
}
}
|
...
This compliant solution accepts an argument of type double
instead of an argument of type float
. In FP-strict mode, 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.
Code Block | ||
---|---|---|
| ||
strictfp class WideSample {
public static int subDoubleFromInt(int op1, double op2) {
return op1 - (int)op2;
}
public static void main(String[] args) {
int result = subDoubleFromInt(1234567890, 1234567890);
// Works as expected
System.out.println(result);
}
}
|
...
Automatic detection of casts that can lose precision is straightforward. Sound determination of whether those casts correctly reflect the intent of the programmer is infeasible in the general case. Heuristic warnings could be useful.
Related Guidelines
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
[JLS 2005] | |
[Seacord 2015] | NUM13-J. Avoid loss of precision when converting primitive integers to floating-point LiveLesson |