[clang] [clang] Do not diagnose float-equal for 0 and 1 (PR #207288)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 6 09:12:31 PDT 2026
https://github.com/Sirraide requested changes to this pull request.
Candidly, I don’t think this is a good idea.
> The values 0 and 1 are commonly used for checking if something is initialized and are both exactly representable as floats. Emitting a diagnostic when comparing with these values seems a bit obtuse.
Assuming you’re talking about `-Wfloat-equal` here, I don’t quite agree w/ this notion: the reason we diagnose this is that computations involving floating-point numbers are fundamentally inexact.
A computation that mathematically would result in 0 or 1 may not actually give you 0 or 1 if you’re using floating-point numbers (e.g. `.7 + .2 + .1 == 1` evaluates to `false` because the LHS is actually `.9999999999999999`), which means that most of the time, `==` or `!=` are definitely not what you want when you’re working with floating-point numbers.
Whether or not such a comparison happens to involve 1 or 0 doesn’t really make a difference here—if anything _not_ diagnosing a comparison with 0 would be _especially_ bad given that comparisons against 0 are actually quite common (e.g. two vectors are perpendicular if their dot product is 0).
(As an aside, from an implementation perspective, this would still need a test and a release note.)
https://github.com/llvm/llvm-project/pull/207288
More information about the cfe-commits
mailing list