...
As in the first compliant example, the value of c cannot be represented exactly as a double. Consequently, the value of f is not precise before or after the multiplication. The explanation and consequences are identical to those stated previously.
Exceptions
Applicability
Improper conversions between integers and floating-point values can yield unexpected results, especially from precision loss. In some cases, these unexpected results can involve overflow or undefined behavior.
NUM50-EX0: It is acceptable to perform operations using a mix of integer and floating-point values when deliberately exploiting the properties of integer arithmetic before conversion to floating point. For example, using integer arithmetic eliminates the need to use the floor() method. Any such code must be clearly documented to help future maintainers understand that this behavior is intentional.
Risk Assessment
Improper conversions between integers and floating-point values can yield unexpected results, especially from precision loss. In some cases, these unexpected results can involve overflow or undefined behavior.
Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
NUM50-JG | low | probable | low | P6 | L2 |
Automated Detection
Automated detection is not feasible in the general case. Heuristic checking could be useful.
Related Guidelines
The CERT C Secure Coding Standard | FLP33-C. Convert integers to floating point for floating point operations |
The CERT C++ Secure Coding Standard | FLP33-CPP. Convert integers to floating point for floating point operations |
Bibliography
...