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 conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in _IEEE Standard for Binary Floating-Point Arithmetic_, ANSI/IEEE Standard 754-1985 (IEEE, New York)" (\[[JLS 2005|AA. Bibliography#JLS 05]\], [Section 4.2.3|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3], "Floating-Point Types, Formats, and Values"). Each of the floating point types has a fixed, limited number of mantissa bits. Consequently, it is impossible to precisely represent any irrational number (_e.g._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.

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

...

Unfortunately, because of the 1/10 cannot be represented precisely in any binary floating point format, this program prints:

A dollar less 7 dimes is $0.29999999999999993

...

An alternative approach is to use the BigDecimal type, though but it is less efficient.

Code Block
bgColor#ccccff
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 a representation other than floating point may can allow for more precision and accuracy for critical arithmetic.

...

Search for vulnerabilities resulting from the violation of this guideline on the CERT website.

Other Languages

Related Guidelines

This guideline appears in the C Secure Coding Standard as : FLP02-C. Avoid using floating point numbers when precise computation is needed.This guideline appears in

the C++ Secure Coding Standard as : FLP02-CPP. Avoid using floating point numbers when precise computation is needed.

Bibliography

Wiki Markup
\[[Bloch 2008|AA. Bibliography#Bloch 08]\] Item 48: Avoid {{float}} and {{double}} if exact answers are required
\[[Bloch 2005|AA. Bibliography#Bloch 05]\] Puzzle 2: Time for a Change
\[[Goldberg 1991|AA. Bibliography#Goldberg 91]\]
\[[JLS 2005|AA. Bibliography#JLS 05]\] [Section 4.2.3, Floating-Point Types, Formats, and Values|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]

...