Versions Compared

Key

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

Using integer arithmetic to calculate a value for assignment to a floating-point variable may lead to loss of information. This can be avoided by converting one of the integers in the expression to a floating-point type. When converting integers to floating-point values, it is important to be aware that there may be loss of precision. (see See guideline INT03-J. Do not cast numeric types to wider floating-point types without range checking.).

Noncompliant Code Example

...

This compliant solution eliminates the initialization errors by storing the integers in the floating-point variables and then performing the arithmetic operations. This ensures that at least one of the operands is a floating-point number, and, consequently, the operation is performed on floating-point numbers.

...

FLP31-EX1: If it is the programmer's intention to have the operation use integers before the conversion (for example, obviating the need to use the floor() method, for example) it should be clearly documented to help future maintainers understand that this behavior is intentional.

...

Improper conversions between integers and floating point values may yield unexpected results, especially as a result of precision loss. In some cases, these unexpected results may involve overflow , or undefined behavior.

Rule Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

FLP02-J

low

probable

low

P6

L2

...