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 type.

...

In this non-compliant code, the floating point variables d, e, and f are not initialized correctly because the operations take place before the values are converted to floating point values and hence the results are truncated to the nearest decimal point or may overflow. Consequently, the division and multiplication operations take place on integers and then get converted to floating point.

...

In this compliant code, the decimal error in initialization is eliminated by first storing the integer in the floating point variable and then performing the arithmetic operation. This ensures that at least one of the operands is a floating point number, and consequently the operation is performed on floating point numbers.

...

Risk Assessment

It may be desirable for to have the operation to take place as integers before the conversion (obviating the need for a trunc() call, for example). In such casesIf that is done, it should be clearly documented to avoid help future maintainers misunderstanding understand the intent of the code.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FLP33-C

1 (low)

2 (probable)

3 (low)

P6

L2

...