Versions Compared

Key

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

Wiki Markup
The Java language provides two primitive types, {{float}} and {{double}}, which are associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations \[[IEEE 754|AA. Bibliography#IEEE 754 2006]\]. Each of the floating-point types has a fixed, limited number of mantissa bits. Consequently, it is impossible to precisely represent any irrational number (for example, pi). Further, because these types use a binary mantissa, they cannot precisely represent many finite decimal numbers, such as 1/10, because these numbers have repeating binary representations.

If precise computation is necessary, such as when performing currency calculations, then floating-point types must not be used, Instead, use an alternative representation that is able to completely represent the necessary values.

Wiki Markup
If precise computation is not necessary, floatin-gpoint representations may be used. In these cases
Wiki Markup
Avoid using the primitive floating-point types when precise computation is necessary, especially when performing currency calculations. Instead, consider alternative representations that are able to completely represent the necessary values. Whatever representation you choose, you must carefully and methodically estimate the maximum cumulative error of the computations to ensure that the resulting error is within acceptable tolerances. Consider using numerical analysis to properly understand the problem. See \[[Goldberg 1991|AA. Bibliography#Goldberg 91]\] for an introduction to this topic.

Noncompliant Code Example

This noncompliant code example performs some basic currency calculations.

...

Code Block
A dollar less 7 dimes is $0.29999999999999993

Compliant Solution

This compliant solution uses an integer type (such as long) and works with cents rather than dollars.

...

Code Block
A dollar less 7 dimes is 30 cents

Compliant Solution

This compliant solution uses the BigDecimal type which provides exact representation of decimal values. Note that on most platforms computations performed using BigDecimal are less efficient than those performed using primitive types. The importance of this reduced efficiency is application-specific.

...

Code Block
A dollar less 7 dimes is $0.3

Risk Assessment

Using a floating-point representation representations when precise computation is required can result in a loss of precision and accuracy when precise computation is required.incorrect values

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

NUM04-J

low

probable

high

P2

L3

Automated Detection

Automated detection of floating-point arithmetic is straight-forward; determining which code suffers from insufficient precision is not feasible in the general case. Heuristic checks, such as flagging floating-point literals that cannot be represented precisely, could be useful.

Related Guidelines

The CERT C Secure Coding Standard

FLP02-C. Avoid using floating point numbers when precise computation is needed

The CERT C++ Secure Coding Standard

FLP02-CPP. Avoid using floating point numbers when precise computation is needed

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d45d74a1fdfdebbb-8f1a2aa6-41934e13-93908efd-db097c3bb76928577ca8d0ba"><ac:plain-text-body><![CDATA[

[ISO/IEC TR 24772:2010

http://www.aitcnet.org/isai/]

"Floating?point Arithmetic [PLF]"

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

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="998c0d1b20eab8f5-0178916d-424d4beb-aaab8906-99f29483019dc12488f1d3e0"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 48: Avoid float and double if exact answers are required

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8c4ea8a171b6e11f-868eb80f-4cae41de-a4b6a74c-a06c08b8dea815f017d9eda4"><ac:plain-text-body><![CDATA[

[[Bloch 2005

AA. Bibliography#Bloch 05]]

Puzzle 2: Time for a Change

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3d1ad4e1feee2c23-f204ce0c-41664810-b6d9b2fb-9bd97bdfc3d2db37ee74ee53"><ac:plain-text-body><![CDATA[

[[Goldberg 1991

AA. Bibliography#Goldberg 91]]

 

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="52a66d1fc28513b0-4df6e63f-452349c8-a605b836-0cd4c3a576a2ed9465a99e75"><ac:plain-text-body><![CDATA[

[[IEEE 754

AA. Bibliography#IEEE 754 2006]]

 

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="945f94ac7570c8ef-b7718acb-4d484317-84c2b06d-f71d5ca19f418b9ede6c61dd"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§4.2.3, "Floating-Point Types, Formats, and Values"

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]

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

...