My pet project uses -nostdlib so I can’t use any C libraries however I can include them for constants.
Below is my code. If I compile using gcc -Wfloat-equal -nostdlib -O2 -c a.c on gcc or clang it’ll give me the error below. I was curious if there’s a way to check without triggering this warning. I suspect I can’t unless I call code I don’t compile
warning: comparing floating-point with ‘==’ or ‘!=’ is unsafe [-Wfloat-equal]
#include <math.h>
int IsInfinity(double d) {
return d != (double)INFINITY && (float)d == INFINITY;
}
>Solution :
You can use return d < INFINITY && (float) d >= INFINITY;.
Another option is return isfinite(d) && isinf((float) d); if you want to classify negative values symmetrically. (These are macros defined in math.h.)