-Wfloat-equal and comparison to zero
Sad Clouds
cryintothebluesky@gmail.com
Tue Nov 12 14:29:20 GMT 2024
On Mon, 11 Nov 2024 21:14:43 +0000 (UTC)
Joseph Myers <josmyers@redhat.com> wrote:
> I don't think this has anything to do with whether one operand of the
> comparison is a constant. It's still the case when comparing with 0.0
> that it's OK if your algorithm is designed such that the other operand is
> exact, and questionable if it is an approximation.
Division by 0.0 is somewhat undefined, so should be avoided. One way of
checking it is with the equality operator. So whether one of the
operands is exact or approximation is irrelevant, since we may only be
interested in preventing division by 0.0.
So having -Wfloat-equal is desirable for catching other cases, but the
assumption of "if (divisor == 0.0) else ..." always being unsafe is a
bit pedantic. In this context, the if statement reads: "I don't care
what approximation divisor contains, as long as it is not exactly 0.0
which would cause division to produce Inf or NaN.
Maybe there are other tricks to perform this check without the equality
operator, but it seems to be the most efficient.
More information about the Gcc
mailing list