Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Note the two added exceptions. Is the wording for EX2 sufficient?

The Java language allows platforms to use available floating point hardware that can provide floating point support with mantissas and/or exponents that contain more bits than the standard Java primitive type double (in the absence of the strictfp modifier). Consequently, these platforms can represent a superset of the values that can be represented by the standard floating point types. Floating point computations on such platforms can produce different results than would be obtained if the floating point computations were restricted to the standard representations of float and double. According to the JLS, Section 15.4, "FP-Strict Expressions"

the net effect of non-fp-strict evaluation, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.

When it is important to obtain Programs that require consistent results from floating point operations across different JVMs and platforms , must use the strictfp modifier. This modifier requires the JVM and platform to behave as though all floating point computations were performed using values limited to those representable by a standard Java float or double, thus consequently guaranteeing that the result of the computations will match exactly across all JVMs and platforms.

Use of the strictfp modifier has no impact leaves execution unchanged on platforms that lack platform-specific floating point behavior. It can have substantial impact, however, on both the efficiency and the result values of floating point computations when executing on platforms that implement platform-specific floating point behavior. On these platforms, use of the strictfp modifier increases the likelihood that intermediate operations will overflow or underflow because it restricts the representable range and precision of intermediate values; it can also reduce computational efficiency. These issues are unavoidable when portability is the main concern.

...

An expression is strict when any of the containing classes, methods, or interfaces is declared to be a strictfp. Constant expressions containing floating point operations are also evaluated strictly. All compile-time constant expressions are by default, strictfp.

...

This noncompliant code example does not mandate strictfp computation. Double.MAX_VALUE is being multiplied by 1.1 and reduced back by dividing by 1.1, according to the evaluation order. If Double.MAX_VALUE is indeed the maximum value permissible by the platform, the calculation will yield the result infinity.

...

The lost magnitude would also have been lost if the value were stored to memory, for example to a field of type float.

Compliant Solution

This compliant solution uses the strictfp keyword to require exact conformance with standard Java floating point. Consequently, the intermediate value of both computations of f * g will be identical to the value stored in this.d, even on platforms that extended range exponents.

Code Block
bgColor#ccccff
strictfp class Example {
  double d = 0.0;

  public void example() {
    float f = Float.MAX_VALUE;
    float g = Float.MAX_VALUE;
    this.d = f * g;
    System.out.println("d (" + this.d ") might not be equal to " + (f * g));
  }

  public static void main(String[] args) {
    Example ex = new Example();
    ex.example();
  }
}

Exceptions

NUM09-EX1: This guideline applies only to calculations that require consistent floating point results on all platforms. Applications that lack this requirement need not comply.

NUM09-EX2: The strictfp modifier may be omitted when competent numerical analysis demonstrates that the computed values will meet all accuracy and behavioral requirements that are appropriate to the application. Note that "competent numerical analysis" generally requires a specialized professional numerical analyst; lesser levels of rigor fail to qualify for this exception.

Risk Assessment

Failure to use the strictfp modifier can result in implementation-defined behavior with respect to the behavior of floating point operations.

...