Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check for infinity while using -Wfloat-equal?

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading