Versions Compared

Key

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

The Java language allows platforms to use available floating-point hardware that can provide floating-point support with 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 Java Language Specification (JLS), Section 15§15.4, "FP-Strict strict Expressions",

Wiki Markup
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.

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 the platform to behave as though all floating-point computations were performed using values limited to those representable that can be represented by a standard Java float or double, consequently guaranteeing that the result of the computations will match exactly across all JVMs and platforms.

Use of Using the strictfp modifier 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 using the strictfp modifier increases the likelihood that intermediate operations will overflow or underflow because it restricts the representable range that can be represented and the precision of intermediate values; it can also reduce computational efficiency. These issues are unavoidable when portability is the main concern.

...

Note that this compliant solution also specifies that the floating-point literals (1.1) are of type double to clarify their expected type; this complies with guideline "NUM18-J. Be aware of numeric promotion behavior."

Noncompliant Code Example

On platforms whose native floating-point hardware provides greater precision than double, the JIT is permitted to use floating-point registers to hold values of type float or type double (in the absence of the strictfp modifier), even though the registers support values with greater exponent range than that of the primitive types. Consequently, conversion from float to double can cause an effective loss of magnitude.

...

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.

...

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

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

NUM09-J

low

unlikely

high

P1

L3

...

Sound automated detection of violations of this guideline are is not feasible in the general case.

Related

...

Search for vulnerabilities resulting from the violation of this guideline on the CERT website.

Related Guidelines

C Secure Coding Standard "FLP00-C. Understand the limitations of floating point numbers"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup

...

" ac:schema-version="1" ac:macro-id="c216a819-c7ca-4017-bc7c-ac38251a36c6"><ac:plain-text-body><![CDATA[

[[Darwin

...

2004

...

AA.

...

Bibliography#Darwin

...

04]

...

]

...

Ensuring

...

the

...

Accuracy

...

of

...

Floating-Point

...

Numbers

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2573fa65-f181-4b06-8276-9ee54adf688e"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§15.4, "FP-strict Expressions"

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.4]

...

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="37f7678e-08f5-4a2f-b1b5-be9f532c1c06"><ac:plain-text-body><![CDATA[

[[JPL 2006

AA. Bibliography#JPL 06]]

9.1.3.

...

Strict

...

and

...

Non-Strict

...

Floating-Point

...

Arithmetic

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a817b979-7e49-42a4-a64e-18ef80c01359"><ac:plain-text-body><![CDATA[

[[McCluskey 2001

AA. Bibliography#McCluskey 01]]

Making Deep Copies of Objects, Using strictfp, and Optimizing String Performance

]]></ac:plain-text-body></ac:structured-macro>

...

NUM08-J. Do not use denormalized numbers      03. Numeric Types and Operations (NUM)      NUM10-J. Do not attempt comparisons with NaN