warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
lkundrak opened this issue · comments
We like to build network-manager-applet's libnm with many compiler warnings enabled. Now that Gtk 4.0 drags in graphene, we now get some extra ones:
[lkundrak@belphegor linux]$ cat test.c
#include <gtk/gtk.h>
[lkundrak@belphegor linux]$ cc $(pkg-config --cflags gtk+-4.0) -Wfloat-equal -c test.c
In file included from /usr/include/graphene-1.0/graphene.h:35,
from /usr/include/gtk-4.0/gsk/gsktypes.h:25,
from /usr/include/gtk-4.0/gsk/gskrenderer.h:26,
from /usr/include/gtk-4.0/gsk/gsk.h:24,
from /usr/include/gtk-4.0/gtk/gtkwidget.h:33,
from /usr/include/gtk-4.0/gtk/gtkapplication.h:27,
from /usr/include/gtk-4.0/gtk/gtkwindow.h:33,
from /usr/include/gtk-4.0/gtk/gtkdialog.h:32,
from /usr/include/gtk-4.0/gtk/gtkaboutdialog.h:30,
from /usr/include/gtk-4.0/gtk/gtk.h:31,
from test.c:1:
/usr/include/graphene-1.0/graphene-simd4f.h: In function ‘graphene_simd4f_is_zero3’:
/usr/include/graphene-1.0/graphene-simd4f.h:1730:36: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1730 | return graphene_simd4f_get_x (v) == 0.f &&
| ^~
/usr/include/graphene-1.0/graphene-simd4f.h:1731:36: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1731 | graphene_simd4f_get_y (v) == 0.f &&
| ^~
/usr/include/graphene-1.0/graphene-simd4f.h:1732:36: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1732 | graphene_simd4f_get_z (v) == 0.f;
| ^~
/usr/include/graphene-1.0/graphene-simd4f.h: In function ‘graphene_simd4f_is_zero2’:
/usr/include/graphene-1.0/graphene-simd4f.h:1749:36: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1749 | return graphene_simd4f_get_x (v) == 0.f &&
| ^~
/usr/include/graphene-1.0/graphene-simd4f.h:1750:36: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1750 | graphene_simd4f_get_y (v) == 0.f;
| ^~
In file included from /usr/include/graphene-1.0/graphene.h:36,
from /usr/include/gtk-4.0/gsk/gsktypes.h:25,
from /usr/include/gtk-4.0/gsk/gskrenderer.h:26,
from /usr/include/gtk-4.0/gsk/gsk.h:24,
from /usr/include/gtk-4.0/gtk/gtkwidget.h:33,
from /usr/include/gtk-4.0/gtk/gtkapplication.h:27,
from /usr/include/gtk-4.0/gtk/gtkwindow.h:33,
from /usr/include/gtk-4.0/gtk/gtkdialog.h:32,
from /usr/include/gtk-4.0/gtk/gtkaboutdialog.h:30,
from /usr/include/gtk-4.0/gtk/gtk.h:31,
from test.c:1:
/usr/include/graphene-1.0/graphene-simd4x4f.h: In function ‘graphene_simd4x4f_inverse’:
/usr/include/graphene-1.0/graphene-simd4x4f.h:900:35: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
900 | if (graphene_simd4f_get_x (det) != 0.f)
| ^~
[lkundrak@belphegor linux]$
I have no idea how to avoid the warning, or even whether comparing a float to zero is actually a bad idea, but we'd be super thankful if it could be avoided.
Thanks
Lubo
The way we use float comparisons is, in theory, safe because we're only using 0. Of course, GCC doesn't know that, so the warning is avoidable by using -Wno-float-equal
.
It would be better to avoid that, but while it's easy to fix Graphene itself—and I have a work in progress branch that does that—we use g_assert_cmpfloat()
a lot in the test suite, and that will need some more work to move to fuzzy comparisons.