Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.CAST.FLOAT_INT

Cast of floating point expression to integral type.

MISRA-C++ Rule 5-0-7 (required): There shall be no explicit floating-integral conversions of a cvalue expression.

This rule is also covered by MISRA.CAST.INT_FLOAT.

Rationale

A cast applied to the result of an expression does not change the type in which the expression is evaluated, which may be contrary to developer expectations.

Example

// Integral to Float
void f1 ( )
{
   int16_t   s16a;
   int16_t   s16b;
   int16_t   s16c;
   float32_t f32a;
   // The following performs integer division
   f32a = static_cast< float32_t > ( s16a / s16b );   // Non-compliant

   // The following also performs integer division
   s16c = s16a / s16b; 
   f32a = static_cast< float32_t > ( s16c );          // Compliant

   // The following performs floating-point division
   f32a = static_cast< float32_t > ( s16a ) / s16b;   // Compliant
}

In the above example, the expression ( s16a / s16b ) is performed with an 'underlying type' of 'int16_t' rather than 'float32_t'.

// Float to Integral
void f2 ( )
{
   float32_t f32a;
   float32_t f32b;
   float32_t f32c;
   int16_t   s16a;
   // The following performs floating-point division
   s16a = static_cast< int16_t > ( f32a / f32b );   // Non-compliant
   // The following also performs floating-point division
   f32c = f32a / f32b;
   s16a = static_cast< int16_t > ( f32c );          // Compliant
   // The following performs integer division
   s16a = static_cast< int16_t > ( f32a ) / f32b;   // Compliant
}

In the above example, the expression ( f32a / f32b ) is performed with an 'underlying type' of 'float32_t' rather than 'int16_t'.