Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2020.2
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 representations.

...

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

Automated detection of floating-point arithmetic is straightforward. 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
java:
Parasoft_V
java:
Parasoft_V
PB.NUM.UBD
Implemented
Do not use "float" and "double" if exact answers are required

Related Guidelines

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.2.3, Floating-Point Types, Formats, and Values

[Seacord 2015]

...


...