On Sun, 10 Nov 2024, Jonathan Wakely via Gcc wrote:

> But 1 - (10 * 0.1) won't, and so the warning is pointing out that any exact
> equality comparisons can be affected by this kind of problem. If you don't
> like the warning, don't enable it.

I think OP's questions are in good faith and your last statement is unnecesarily
passive-aggressive. If we respond to queries like that, it just reflects badly
on us, because the implication is that we are not interested in criticisms for
diagnostics or clarifying their behavior.

Our warning is extremely simplistic, it warns even for 'x != x', and even the
wording of the warning is bad: it says '... is unsafe' but there are no safety
concerns here.

I can see situations where warning for floating-point comparison makes sense
even by default:

  float x;
  ...
  if (x != 0.1) // not 0.1f

since 0.1f != 0.1 this could yield something like

  warning: comparison is always true

but warning on every comparison is indefensible.

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

Their behavior is not documented, and it is not completely straightforward to me
looking at the source, but they do not issue the warning if the value is exactly
representable in the format (but they warn if it was implicitly cast from an
integer, like in 'x == 0', which is probably a bug). They also suppress the
warning in some other cases, e.g. for 'x == x'.

Alexander

Reply via email to