Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Wiki MarkupThe Java language provides two primitive floating-point types, {{float}} and {{double}}, which are associated with the single-precision 32-bit and double-precision 64-bit format values and operations specified by IEEE 754 \ [[IEEE 754|AA. References#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 0.1, because these numbers have repeating binary representations.

When precise computation is necessary, such as when performing currency calculations, floating-point types must not be used. Instead, use an alternative representation that can completely represent the necessary values. unmigrated-wiki-markup

When precise computation is unnecessary, floating-point representations may be used. In these cases, 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's work for an introduction to this topic \ [[Goldberg 1991|AA. References#Goldberg 91]\].

Noncompliant Code Example

...

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="c0f3a11d-c11a-4477-9c9b-159def5e7699"><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="9977931c-2276-4531-9b87-e6a185c727d6"><ac:plain-text-body><![CDATA[[[Bloch 2008AA. References#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="7baf5ba7-9a6f-4e19-83ec-9f6a1df09382"><ac:plain-text-body><![CDATA[

[[Bloch 2005AA. References#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="bb9f3fb8-505e-49f3-a96c-f260c647c2f8"><ac:plain-text-body><![CDATA[

[[Goldberg 1991AA. References#Goldberg 91]]

 

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7de707a9-066e-4d72-ad31-a2117f6529d7"><ac:plain-text-body><![CDATA[

[[IEEE 754AA. References#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="36a48d0e-589c-4345-89f3-6fb81dca2492"><ac:plain-text-body><![CDATA[

[[JLS 2005AA. References#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>

...

      03. Numeric Types and Operations (NUM)      NUM05-J. Do not use denormalized numbers