Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The following 19 specific conversions on primitive types are called the widening primitive conversions:

  • byte to short, int, long, float, or double
  • short to int, long, float, or double
  • char to int, long, float, or double
  • int to long, float, or double
  • long to float or double
  • float to double

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 The Java Language Specification (JLS) requires that the conversion and rounding occur silently, that is, without any runtime exception . See (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.

Note that conversions from float to double can also lose information about the overall magnitude of the converted value . See (see rule NUM53-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. In FP-strict mode, values of type float have 23 mantissa bits, a sign bit, and an 8-bit exponent . See (see rule 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
bgColor#FFcccc
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);  
  }

}

Note that conversions from long to either float or double can lead to similar loss of precision.

...

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
bgColor#ccccff
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);  
  }
}

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

NUM13-J

lowLow

unlikelyUnlikely

mediumMedium

P2

L3

Automated Detection

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.

...