Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

If a floating-point value is to be demoted to a floating-point value of a smaller range and precision or to an integer type, or if an integer type is to be converted to a floating-point type, the value must be represented in the new type.

Wiki MarkupSection 6.3.1.4 of C99 \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] says

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.

...

Tool

Version

Checker

Description

Section

Fortify SCA

Section

V. 5.0

 

Section

can detect violations of this rule with CERT C Rule Pack

Section

Compass/ROSE

 

 

Section

can detect some violations of this rule. However, it does not flag implicit casts, only explicit ones

Section

Coverity Prevent

Include Page
c:Coverity_Vc:
Coverity_V
Section

MISRA_CAST

Section

can detect instances where implicit float conversion is involved; e.g. implicitly converting complex expression with integer type to floating type, implicitly converting a double expression to narrower float type may lose precision, implicitly converting complex expression from float to double, implicit conversion from float to double in a function argument etc.

...

MITRE CWE: CWE-681, "Incorrect Conversion between Numeric Types"

Bibliography

...

\[[IEEE 754|AA. Bibliography#IEEE 754 2006]\] IEEE 754-1985 Standard for Binary Floating-Point Arithmetic

...

FLP34-C. Ensure that floating point conversions are within range of the new type      FLP34-C. Ensure that floating point conversions are within range of the new type      FLP36-C. Beware of precision loss when converting integral types to floating point