TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
TIOBE - C Coding Standard

Guideline:  6.3#2Checked automatically with code checker

Synopsis:Floating values should not be converted to integral types except through use of standard library routines.
Severity Level:2


Since mixed precision arithmetic involves implementation defined and undefined behaviour, it is safer to avoid implicit conversions between floating and integral types.

For ANSI-C 99 compilers this comes down to using the function "lrint" from "<math.h>". For other C compilers an own library function should be written that performs float to integer conversions. This library function could be defined as follows.


long f2i(double f) {
  return (long) floor(f + 0.5); //TICS !6.3#2: we know what we are doing

So instead of using an implicit conversion such as

int i;
float fl;
i = fl; /* implicit conversion from float to int */

one should make it explicit by using the defined library function:

int i;
float fl;
i = f2i(fl); /* explicit conversion from float to int */