[Bug c/93878] New: -Wfloat-equal complains even in safe cases
roland.illig at gmx dot de
gcc-bugzilla@gcc.gnu.org
Sat Feb 22 12:36:00 GMT 2020
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93878
Bug ID: 93878
Summary: -Wfloat-equal complains even in safe cases
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: roland.illig at gmx dot de
Target Milestone: ---
$ cat float.c
#include <math.h>
int f(float a, float b) {
float rounded = roundf(b);
if (a < 0) return 1;
if (a == 3.0) return 2;
if (rounded == 3.0) return 3;
if (a == b) return 4;
return 5;
}
$ gcc -Wall -Os -Wextra -Wfloat-equal -c float.c
float.c: In function ‘f’:
float.c:6:11: warning: comparing floating point with == or != is unsafe
[-Wfloat-equal]
6 | if (a == 3.0) return 2;
| ^~
float.c:7:17: warning: comparing floating point with == or != is unsafe
[-Wfloat-equal]
7 | if (rounded == 3.0) return 3;
| ^~
float.c:8:11: warning: comparing floating point with == or != is unsafe
[-Wfloat-equal]
8 | if (a == b) return 4;
| ^~
The case "a == 3.0" may be debatable. But the case "rounded == 3.0" is
perfectly safe since the number is rounded before being compared.
These test cases should be added to the test suite for this diagnostic, to
demonstrate the current behavior and limitations.
There are probably many other situations that are safe as well, therefore it's
good that -Wfloat-equal is neither included in -Wall nor in -Wextra. Thanks for
that. :)
More information about the Gcc-bugs
mailing list