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]]. 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.
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]] for an introduction to this topic.
Noncompliant Code Example
This noncompliant code example performs some basic currency calculations.
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 .10 lacks an exact representation in either Java floating-point type (or any floating-point format that uses a binary mantissa) this program prints
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.
long dollar = 100; long dime = 10; int number = 7; System.out.println ("A dollar less " + number + " dimes is " + (dollar - number * dime) + " cents" );
This code correctly outputs:
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.
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) )) );
This code outputs:
A dollar less 7 dimes is $0.3
Risk Assessment
Using a floating-point representation can result in a loss of precision and accuracy when precise computation is required.
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
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="83913810-e753-4bc5-a1d4-3e43e743bbfa"><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="abbcb1b3-bf45-4046-a269-23db3b0da9cc"><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="f7116845-3588-4e74-81be-e4009d764ccc"><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="271fb41c-6652-433c-abb1-ddea3567312b"><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="c25b6ad5-5ac1-48f1-a145-791077593a3c"><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="504130b7-1774-4c93-90bc-fac940f84e8a"><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> |
NUM03-J. Provide mechanisms to handle unsigned data when required 03. Numeric Types and Operations (NUM) NUM05-J. Do not use denormalized numbers