On Sun, 10 Nov 2024 09:45:41 +0000
Jonathan Wakely <jwakely....@gmail.com> wrote:

> Does clang only have a special case for 0.0, or for any literal value?
> 

It looks like clang can detect which floating point literals can be
represented exactly and does not generate any warnings for those.

$ cat test.c
#include <stdio.h>

int main(void)
{
        double value = 0.0;

        if (value == 0.0)
        {
                printf("value == 0.0\n");
        }

        if (value == 1.0)
        {
                printf("value == 1.0\n");
        }

        if (value == 1.1)
        {
                printf("value == 1.1\n");
        }
}

$ clang -Wfloat-equal test.c
test.c:17:12: warning: comparing floating point with == or != is unsafe 
[-Wfloat-equal]
        if (value == 1.1)
            ~~~~~ ^  ~~~
1 warning generated.

As suggested by Andrew, I used GCC pragmas to silence the warnings for
specific lines of code. So I get the benefit of -Wfloat-equal checks
without the spurious cases.

Thanks.

Reply via email to