Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Note
titleDeprecated

This rule may be deprecated and replaced by a similar guideline.

06/28/2014 -- Version 1.0


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]. 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 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. 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.

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. Bibliography#Goldberg 91]\].

Noncompliant Code Example

This noncompliant code example performs some basic currency calculations.:

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), on most platforms, this program prints the following:

Code Block

A dollar less 7 dimes is $0.29999999999999993

...

This compliant solution uses an integer type (such as long int) and works with cents rather than dollars.:

Code Block
bgColor#ccccff

longint dollar = 100;
longint dime = 10;
int number = 7;
System.out.println(
  ("A dollar less " + number + " dimes is $0." +
		    (dollar - number * dime) + " cents" 
);

This code correctly outputs the following:

Code Block

A dollar less 7 dimes is $0.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. 

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) )) );

This code outputs the following:

Code Block

A dollar less 7 dimes is $0.3

...

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

Low

probable

Probable

high

High

P2

L3

Automated Detection

Automated detection of floating-point arithmetic is straight forwardstraightforward. 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.

ToolVersionCheckerDescription
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.NUM04.UBDDo not use "float" and "double" if exact answers are required

Related Guidelines

[

The CERT C Secure Coding Standard

FLP02-C. Avoid using floating-point numbers when precise computation is needed

The CERT C++ Secure Coding Standard

VOID 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="821351b7-2aff-4540-8988-24a4250b0522"><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

Android Implementation Details

The use of floating-point on Android is not recommended for performance reasons.

Bibliography

[Bloch 2008]

Item 48, "Avoid float and double If Exact Answers Are Required"

[Bloch 2005]

Puzzle 2, "Time for a Change"

[Goldberg 1991]


[IEEE 754]


[JLS 2015]

§4

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3240b106-21e8-454e-89cc-10c9bfbe8b20"><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="0f65206c-7e4d-4eb4-b403-05736738e469"><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="628fddb7-2aa9-47f3-b9d7-92e32cb28668"><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="7d8d5b1d-2cce-4bfe-b61d-b5e3059dc448"><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="a4f34810-dc1c-4ba1-9f7e-08e67391bff9"><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>

...

[Seacord 2015]

Image result for video iconImage Added NUM04-

...


...

Image Added Image Added Image Added denormalized numbers