comparison operators
David Hough
uunet!Eng.Sun.COM!David.Hough
Mon Dec 9 12:01:35 PST 1991
While it's possible to make do with function calls and
imperatives to optimizers to reverse-engineer the programmer's intent -
Fortran and C prove that programmers can make do with just about anything -
generally it's a lot easier to write as you think and think as
you write. So I prefer an operator notation
for comparisons. The most natural would be something like
if (x <? y)
where the relational operator is a list of relations chosen from < = > ?
with ? standing for unordered. This corresponds to what happens at the
lowest levels of the hardware.
However it seems that every possible ASCII character has problems as a
designator for the unordered relationship in C. That's why a backup position
is to invent new operators that
exploit ! to mean "not" in this context: !>= being equivalent to <?.
This is different from !(x >= y), because !>= doesn't generate an exception
for an unordered comparison.
I don't have a strong preference either way (! or an unordered relation
character). But I don't think anybody who wants to write code that is
efficient, and portable among IEEE implementations, and raises no gratuitous
exceptions, will thank NCEG if it declines to choose one or the other.
--------------------------------------------------------------------------
Why !(x >= y) differs from (x !>= y):
Some goals of IEEE arithmetic as it developed in 1977-78 were
* it should be possible to run most existing correct programs from other
systems silently and without degrading results,
* existing incorrect programs from other systems should not silently run
incorrectly,
* it should be convenient to write new programs that exploit as many
IEEE arithmetic features as possible, on as many different types of operating
environments as possible, without depending on language extensions
that would be slow in coming and incompatible among various languages
From the goals the design emerged:
Default exception handling is always nonstop.
NaNs never compare equal. Only numbers can be equal.
== and != are unexceptional, since wrong conclusions will rarely be reached,
and can be used with x == x or x != x to distinguish unordered items
< <= > >= are exceptional if used with unordered items, because of a high
probability of reaching erroneous conclusions in old incorrect programs
running nonstop. It was always intended that retrospective diagnostics to
warn of such possibilities would
be provided in operating environments for which they were appropriate.
New operators designed for IEEE arithmetic should not be exceptional in that
sense because they would be used in programs with the possibility of NaNs in
mind.
More information about the Numeric-interest
mailing list