MISRA Discussion Forums

Full Version: Casting integer to floating point
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following code:

int16_t int_torque_I_MUL;
float32_t f_temp1;
f_temp1 = (float32_t)(int_torque_I_MUL);
The above will generate:
MISRA-C:2012 R.10.1,R.10.3,R.10.4,R.10.5,R.11.1: Value is not of appropriate type. : (double and short)
But I don't understand why??
How can I cast a signed 16 bit integer to a double (float32_t )?
Firstly, this is the MISRA C 2004 sub-forum but you posted MISRA C 2012 rules:)
Secondly, i think this is a false positive from whatever tool you are using.
If you look at rule 10.5, a cast from essentially signed to essentially floating is allowed.
In my book that supersedes rule 10.3, although it is not clearly mentioned in the standard.
Rules 10.1 and 10.4 are for arithmetic operations which is not the case here -> tool error.
Rule 11.1 applies to function pointers -> epic tool error:)
The MISRA-C working group can confirm that the cast in the example does not violate any MISRA-C:2004 or MISRA-C:2012 rules.