Wiki Markup |
---|
The 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. 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, because these numbers have repeating binary representations. |
If When precise computation is necessary, such as when performing currency calculations, then floating-point types must not be used, . Instead, use an alternative representation that can completely represent the necessary values.
Wiki Markup |
---|
IfWhen precise computation is not necessaryunnecessary, 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. Bibliography#Goldberg 91]\]. |
...
Code Block | ||
---|---|---|
| ||
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
Code Block |
---|
A dollar less 7 dimes is $0.29999999999999993 |
...
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 | ||
---|---|---|
| ||
import java.math.BigDecimal; BigDecimal dollar = new BigDecimal("1.0"); BigDecimal dime = new BigDecimal("0.1"); int number = 7; System.out.println ("A dollar less " + number + " dimes is $" + (dollar.subtract(new BigDecimal(number).multiply(dime) )) ); |
...
Using floating-point representations when precise computation is required can result in a loss of precision and incorrect values.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
NUM04-J | low | probable | high | P2 | L3 |
...
Automated detection of floating-point arithmetic is straight - forward; . However, 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.
...
FLP02-C. Avoid using floating point numbers when precise computation is needed | ||||
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="69717eb5f585302b-b762daaa-47624307-91fa804f-6b46e37a4b3e6b71dcd141c1"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | " Floating-point 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="fd3ca4f1bf036406-ce458300-4aba48b6-b8aa9d2e-06bebfcca5be1cc0e4aec630"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 48: , Avoid | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8febb218d19d0759-a21e72cb-434e4d7b-8f28b000-310a757a85f3c59ade66a6c2"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. Bibliography#Bloch 05]] | Puzzle 2: , Time for a Changechange | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="67e77f7683313ad0-0888ec4a-4d1c47aa-ba3d9f9f-4db2a62be8c33f5b29940e1e"><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="75ff9935f513f186-2f0cd251-4e3746a9-9de9b5f3-047d190600e7eeac0025bde2"><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="f207af6f448211e8-5c25fad6-40b64b7d-8a208406-9d80679c32af7bb67b590951"><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> |
...