CERT-FLP34-C
In this section:
Synopsis
Ensure that floating-point conversions are within range of the new type
Enabled by default
Yes
Severity/Certainty
Low/Low

Full description
If a floating-point value is to be converted 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 representable in the destination type.
Coding standards
- CERT FLP34-C
Ensure that floating point conversions are within range of the new type
Code examples
The following code example fails the check and will give a warning:
void func(float f_a) {
int i_a;
/* Undefined if the integral part of f_a cannot be represented. */
i_a = f_a;
}
The following code example passes the check and will not give a warning about this issue:
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
extern size_t popcount(uintmax_t); /* See INT35-C */
#define PRECISION(umax_value) popcount(umax_value)
void func(float f_a) {
int i_a;
if (isnan(f_a) ||
PRECISION(INT_MAX) < log2f(fabsf(f_a)) ||
(f_a != 0.0F && fabsf(f_a) < FLT_MIN)) {
/* Handle error */
} else {
i_a = f_a;
}
}