Literal decimal floating-point numbers can not always be represented precisely using the double
primitive type , because the underlying representation of double
is binary. This imprecision becomes apparent when a BigDecimal
is constructed from a double
.
...
This noncompliant code example passes a double
value to the BigDecimal
constructor. Because the decimal literal 0.1
can not cannot be precisely represented by a double
, precision of the BigDecimal
is affected.
...
Using the BigDecimal(double val)
constructor with decimal floating-point literals can lead to loss of precision.
...
Automated detection appears to be straightforward.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
Bibliography
[JLS 2005]
...
NUM12-J. Do not use floating-point variables as loop counters 03. Numeric Types and Operations (NUM) NUM14-J. Do not rely on the default string representation of floating point values