CERT-FLP37-C
In this section:
Synopsis
Do not use object representations to compare floating-point values.
Enabled by default
Yes
Severity/Certainty
Low/Low

Full description
Do not compare floating-point object representations directly, such as by calling memcmp() or its moral equivalents. Instead, the equality operators (== and !=) should be used to determine if two floating-point values are equivalent.
Coding standards
- CERT FLP37-C
Cast the return value of a function that returns a floating point type
Code examples
The following code example fails the check and will give a warning:
#include <stdbool.h>
#include <string.h>
struct S {
int i;
float f;
};
bool are_equal(const struct S *s1, const struct S *s2) {
if (!s1 && !s2)
return true;
else if (!s1 || !s2)
return false;
return 0 == memcmp(s1, s2, sizeof(struct S));
}
The following code example passes the check and will not give a warning about this issue:
#include <stdbool.h>
#include <string.h>
struct S {
int i;
float f;
};
bool are_equal(const struct S *s1, const struct S *s2) {
if (!s1 && !s2)
return true;
else if (!s1 || !s2)
return false;
return s1->i == s2->i &&
s1->f == s2->f;
}