Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: integrated JPCERT comments

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 specified by IEEE 754 \[[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 0.1/10, because these numbers have repeating binary representations.

...

Wiki Markup
If precise computation is not necessary, floatin-gpoint 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. Bibliography#Goldberg 91]\] for an introduction to this topic.

Noncompliant Code Example

...

Code Block
bgColor#FFcccc
double dollar = 1.00;
double dime = 0.10;
int number = 7;
System.out.println ("A dollar less " + number + " dimes is $" +
		    (dollar - number * dime) );

Because the value 0.10 lacks an exact representation in either Java floating-point type (or any floating-point format that uses a binary mantissa) this program prints

...

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="67591e43b533ad80-e26acaf8-4e8344e0-8dbcb6ea-d5a3485ff99c4713318481b1"><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>

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2e756ffc468ddf9a-cc97cae6-4ac94686-9c00a74f-749e894356bfc0e3b95b7712"><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="ee49140abda152ff-7892e890-47764be8-b075b906-d7651cd96a7f220c15e88ff6"><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="85f78422475f7301-02626e5d-422d4aee-9814bb83-6f985663adb3d51bc10fe37c"><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="04d2a8bbb3d464c3-66d42df3-4a82414e-b03e8e52-f95bd01ae30a05e7eeb592c8"><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="51faff6226e43c2e-40a09109-4cda4d65-8fe48349-ac4d18b17b2ab034eb0b768d"><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>

...