Sujet : Re: Interval Comparisons
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 04. Jun 2024, 23:29:05
Autres entêtes
Organisation : None to speak of
Message-ID : <871q5c1gwe.fsf@nosuchdomain.example.com>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Thiago Adams <
thiago.adams@gmail.com> writes:
On 04/06/2024 04:14, Lawrence D'Oliveiro wrote:
Would it break backward compatibility for C to add a feature like this
from Python? Namely, the ability to check if a value lies in an interval:
def valid_char(c) :
"is integer c the code for a valid Unicode character." \
" This excludes surrogates."
return \
(
0 <= c <= 0x10FFFF
and
not (0xD800 <= c < 0xE000)
)
#end valid_char
>
See Chaining Comparisons
https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0893r0.html
>
https://medium.com/@barryrevzin/chaining-comparisons-seeking-information-from-the-audience-abec909a1366
>
I don't know what are the current status of this proposal.
That's a proposal for C++.
One interesting piece of information is that the authors did some research
on existing code:
"""
To that end, we created a clang-tidy check for all uses of chained
comparison operators, ran it on many open source code bases, and
solicited help from the C++ community to run it on their own. The check
itself casts an intentionally wide net, matching any instance of a @ b @
c for any of the six comparison operators, regardless of the types of
these underlying expressions.
Overall, what we found was:
- Zero instances of chained arithmetic comparisons that are correct
today. That is, intentionally using the current standard behavior.
- Four instances of currently-erroneous arithmetic chaining, of the
assert(0 <= ratio <= 1.0); variety. These are bugs that compile today
but don’t do what the programmer intended, but with this proposal would
change in meaning to become correct.
- Many instances of using successive comparison operators in DSLs that
overloaded these operators to give meaning unrelated to comparisons.
"""
I presume they searched only C++ code, but I'd expect similar results
for C.
As indicated above, such a change would quietly break any existing
code that uses something like `a < b < c` that's intended to mean
`(a < b) < c`, but it would quietly *fix* any code that uses `a < b < c`
under the incorrect assumption that the comparisons are chained.
(Though the latter code will not have been tested under the new semantics.)
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */