...
When precise computation is necessary, use alternative representations that can accurately represent the values. For example, if you are performing arithmetic on decimal values and need an exact decimal rounding, represent the values in binary-coded decimal instead of using floating-point values. Another option is decimal floating-point arithmetic, as specified by ANSI/IEEE 754-2007. ISO/IEC WG14 has drafted a proposal to add support for decimal floating-point arithmetic to the C language [ISO/IEC DTR 24732].
When precise computation is necessary, carefully and methodically estimate the maximum cumulative error of the computations, regardless of whether decimal or binary is used, to ensure that the resulting error is within tolerances. Consider using numerical analysis to properly understand the problem. An introduction can be found in [Goldberg 1991].
Noncompliant Code Example
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE |
|
| Can detect violations of this recommendation. In particular, it checks to see if the arguments to an equality operator are of a floating-point type. | PRQA QA-C | |||||
Include Page | PRQA_V | PRQA_V | Partially implemented |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this recommendation on the CERT website.
...
The CERT Oracle Secure Coding Standard for Java: NUM04-J. Do not use floating-point numbers if precise computation is required
ISO/IEC TR 24772 "PLF Floating point arithmetic"
Bibliography
[Goldberg 1991]
[IEEE 754 2006]
...