Relational operator


In computer science, a relational operator is a programming language construct or operator that defines syntactically a relationship between two entities. These include numerical equality and inequalities.
In programming languages that include a distinct boolean data type in their type system, like Pascal, Ada, Python or Java, these operators usually evaluate to true or false, depending on if the conditional relationship between the two operands holds or not.
In languages such as C, relational operators return the integers 0 or 1, where 0 stands for false and any non-zero value stands for true.
An expression created using a relational operator forms what is termed a relational expression or a condition. Relational operators can be seen as special cases of logical predicates.

Equality

Usage

Equality is used in many programming language constructs and data types. It is used to test if an element already exists in a set, or to access to a value through a key. It is used in switch statements to dispatch the control flow to the correct branch, and during the unification process in logic programming.
There can be multiple valid definitions of equality, and any particular language might adopt one or more of them, depending on various design aspects. One possible meaning of equality is that "if a equals b, then either a or b can be used interchangeably in any context without noticing any difference". But this statement does not necessarily hold, particularly when taking into account mutability together with content equality.

Location equality vs. content equality

Sometimes, particularly in object-oriented programming, the comparison raises questions of data types and inheritance, equality, and identity. It is often necessary to distinguish between:
  • two different objects of the same type, e.g., two hands
  • two objects being equal but distinct, e.g., two $10 banknotes
  • two objects being equal but having different representation, e.g., a $1 bill and a $1 coin
  • two different references to the same object, e.g., two nicknames for the same person
In many modern programming languages, objects and data structures are accessed through references. In such languages, there becomes a need to test for two different types of equality:
  • Location equality : if two references reference the same object. Interactions with the object through A are indistinguishable from the same interactions through B, and in particular changes to the object through A are reflected through B.
  • Content equality: if the objects referenced by two references are equivalent in some sense:
The first type of equality usually implies the second, but the converse is not necessarily true. For example, two string objects may be distinct objects but contain the same sequence of characters. See identity for more of this issue.
Real numbers, including many simple fractions, cannot be represented exactly in floating-point arithmetic, and it may be necessary to test for equality within a given tolerance. Such tolerance, however, can easily break desired properties such as transitivity, whereas reflexivity breaks too: the IEEE floating-point standard requires that NaN ≠ NaN holds. In contrast, the private standard for posit arithmetic has a similar concept, NaR, where NaR = NaR holds.
Other programming elements such as computable functions, may either have no sense of equality, or an equality that is uncomputable. For these reasons, some languages define an explicit notion of "comparable", in the form of a base class, an interface, a trait or a protocol, which is used either explicitly, by declaration in source code, or implicitly, via the structure of the type involved.

Comparing values of different types

In JavaScript, PHP, VBScript and a few other dynamically typed languages, the standard equality operator follows so-called loose typing, that is it evaluates to true even if two values are not equal and are of incompatible types, but can be coerced to each other by some set of language-specific rules, making the number 4 compare equal to the text string "4", for instance. Although such behaviour is typically meant to make the language easier, it can lead to surprising and difficult to predict consequences that many programmers are unaware of. For example, JavaScript's loose equality rules can cause equality to be intransitive, or make certain values be equal to their own negation.
A strict equality operator is also often available in those languages, returning true only for values with identical or equivalent types. For languages where the number 0 may be interpreted as false, this operator may simplify things such as checking for zero.

Ordering

Greater than and less than comparison of non-numeric data is performed according to a sort convention which may be built into the programming language and/or configurable by a programmer.
When it is desired to associate a numeric value with the result of a comparison between two data items, say a and b, the usual convention is to assign −1 if a < b, 0 if a = b and 1 if a > b. For example, the C function strcmp performs a three-way comparison and returns −1, 0, or 1 according to this convention, and qsort expects the comparison function to return values according to this convention. In sorting algorithms, the efficiency of comparison code is critical since it is one of the major factors contributing to sorting performance.
Comparison of programmer-defined data types may be carried out by custom-written or library functions, or, in some languages, by overloading a comparison operator – that is, assigning a programmer-defined meaning that depends on the data types being compared. Another alternative is using some convention such as member-wise comparison.

Logical equivalence

Though perhaps unobvious at first, like the boolean logical operators XOR, AND, OR, and NOT, relational operators can be designed to have logical equivalence, such that they can all be defined in terms of one another. The following four conditional statements all have the same logical equivalence E for any given x and y values:
This relies on the domain being well ordered.

Standard relational operators

The most common numerical relational operators used in programming languages are shown below. Standard SQL uses the same operators as BASIC, while many databases allow != in addition to <> from the standard. SQL follows strict boolean algebra, i.e. doesn't use short-circuit evaluation, which is common to most languages below. E.g. PHP has it, but otherwise it has these same two operators defined as aliases, like many SQL databases.
Conventionequal tonot equal togreater thanless thangreater than
or equal to
less than
or equal to
In print=><
FORTRAN.EQ..NE..GT..LT..GE..LE.
ALGOL 68=><
ALGOL 68=/=><>=<=
ALGOL 68eqnegtltgele
APL=><
BASIC, ML, Pascal=<>><>=<=
C-like!=><>=<=
MUMPS='=><'<'>
Lua~=><>=<=
Erlang/=><>==<
Erlang=:==/=><>==<
Bourne-like shells-eq-ne-gt-lt-ge-le
Batch fileEQUNEQGTRLSSGEQLEQ
ooRexx, REXX=¬=><>=<=
ooRexx, REXX=\=><>=<=
ooRexx, REXX=<>><>=<=
MATLAB~=><>=<=
MATLABeqnegtltgele
Fortran 90, Haskell/=><>=<=
Mathematica!=><>=<=
MathematicaEqualUnequalGreaterLessGreaterEqualLessEqual

Other conventions are less common: Common Lisp and Macsyma/Maxima use Basic-like operators for numerical values, except for inequality, which is /= in Common Lisp and # in Macsyma/Maxima. Common Lisp has multiple other sets of equality and relational operators serving different purposes, including eq, eql, equal, equalp, and string=. Older Lisps used equal, greaterp, and lessp; and negated them using not for the remaining operators.