MISRA Discussion Forums
Casting integer to floating point - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.10 Arithmetic Type Conversions (https://forum.misra.org.uk/forumdisplay.php?fid=37)
+---- Thread: Casting integer to floating point (/showthread.php?tid=1280)



Casting integer to floating point - bobvanderputten - 04-10-2016

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 )?


Re: Casting integer to floating point - dg1980 - 05-10-2016

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:)


Re: Casting integer to floating point - misra-c - 09-12-2016

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.