DLVO theory: Difference between revisions
en>Williams12357 No edit summary |
en>John of Reading |
||
Line 1: | Line 1: | ||
{{lead too long|date=September 2013}} | |||
In [[computer science]], a '''relational operator''' is a [[programming language]] construct or [[Operator (programming)|operator]] that tests or defines some kind of [[relation (mathematics)|relation]] between [[Binary function|two entities]]. These include numerical [[equality (mathematics)|equality]] (e.g., 5 = 5) and [[inequalities]] (e.g., 4 ≥ 3). | |||
In programming languages that include a distinct [[boolean data type]] in their [[type system]], like [[Java (programming language)|Java]], these operators return true or false, depending on whether the conditional relationship between the two [[operands]] holds or not. In other languages such as [[C (programming language)|C]], relational operators return the integers 0 or 1. Some programming languages make a [[syntax (programming languages)|syntactical]] distinction between the "equals" of [[assignment (computer science)|assignment]] (e.g. <code>a = 1</code> assigns the value 1 to the variable "a") and the "equals" of comparison (<code>if a == 1 then ...</code>). Other languages determine which is meant from context. | |||
"Greater than" and "less than" comparison of non-numeric data is performed according to a sort convention (such as, for text strings, [[lexicographical order]]) which may be built into the programming language and/or configurable by the 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 <code>[[strcmp]]</code> 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 algorithm]]s, the efficiency of comparison code is critical since it is one of the major factors contributing to sorting performance. | |||
In [[floating-point arithmetic]], numbers, including many simple [[fraction (mathematics)|fraction]]s, cannot be represented exactly, and it may be necessary to test for equality within a given tolerance. | |||
Comparison of programmer-defined [[data type]]s (data types of which the programming language itself has no in-built understanding) may be carried out by custom-written or library functions (such as <code>strcmp</code> mentioned above), or, in some languages, by "[[Operator overloading|overloading]]" a comparison operator – that is, assigning a programmer-defined meaning that depends on the data types being compared. | |||
Sometimes, particularly in [[object-oriented programming]], the comparison raises questions of [[data type]]s and [[inheritance (computer science)|inheritance]], [[equality (mathematics)|equality]] and [[identity (mathematics)|identity]]. It is often necessary to distinguish between: | |||
* two objects with different datatypes both related to another datatype, e.g. an orange and a lemon, both being citrus fruit | |||
* two different objects of the same type, e.g. two hands | |||
* two objects being equal but distinct, e.g. two $10 banknotes | |||
* two different references to the same object, e.g. two nicknames for the same person | |||
Sameness and difference can be relative or graduated as well as absolute, particularly in [[fuzzy logic]], [[artificial intelligence]], [[signal processing]], [[lossy data compression]] and [[pattern recognition]]. | |||
An [[expression (programming)|expression]] created using a relational operator forms what is known as a ''relational expression'' or a ''condition''. Relational operators are also used in technical literature instead of words. Relational operators are usually written in [[infix notation]], if supported by the programming language, which means that they appear between their operands (the two expressions being related). For example, an expression in C will print the message if the ''x'' is less than ''y'': | |||
<source lang="c"> | |||
if (x < y) { | |||
printf("x is less than y in this example\n"); | |||
} | |||
</source> | |||
Other programming languages, such as [[Lisp (programming language)|Lisp]], use [[prefix notation]], as follows: | |||
<source lang="lisp"> | |||
(>= X Y) | |||
</source> | |||
==Standard relational operators== | |||
The most common numerical relational operators used in programming languages are shown below. | |||
{| class="wikitable" style="text-align: center;" | |||
|+ Common relational operators | |||
! Convention | |||
! ''equal to'' | |||
! ''not equal to'' | |||
! ''greater than'' | |||
! ''less than'' | |||
! ''greater than<br>or equal to'' | |||
! ''less than<br>or equal to'' | |||
|- | |||
! In print | |||
| = | |||
| ≠ | |||
| > | |||
| < | |||
| ≥ | |||
| ≤ | |||
|- | |||
<!-- I don't think this row is helpful, as it only makes the table wide | |||
! Meaning | |||
|style="text-align: left;"| Test the equivalence of two values | |||
|style="text-align: left;"| Test the negated equivalence of two values | |||
|style="text-align: left;"| Test if the value of the left expression is greater than that of the right | |||
|style="text-align: left;"| Test if the value of the left expression is less than that of the right | |||
|style="text-align: left;"| Test if the value of the left expression is greater than or equal to that of the right | |||
|style="text-align: left;"| Test if the value of the left expression is less than or equal to that of the right | |||
|- | |||
--> | |||
! rowspan="1" | [[Fortran]] <ref group="note">Recent versions of Fortran also allow C-like relational operators.</ref> | |||
| <code>.EQ.</code> | |||
| <code>.NE.</code> | |||
| <code>.GT.</code> | |||
| <code>.LT.</code> | |||
| <code>.GE.</code> | |||
| <code>.LE.</code> | |||
|- | |||
! rowspan=3 | [[ALGOL 68]] <ref group="note">[[ALGOL 68]]: "[[Stropping (syntax)|stropping]]" regimes are used in code on platforms with limited character sets (e.g. use <code>>=</code> or <code>GE</code> instead of <code>≥</code>), platforms with no <code>'''bold'''</code> [[Emphasis (typography)|emphasis]] (use <code>'ge'</code>), or platforms with only [[UPPERCASE]] (use <code>.GE</code> ''or'' <code>'GE'</code>).</ref> | |||
|rowspan=2| <code>=</code> | |||
| <code>≠</code> | |||
|rowspan=2| <code>></code> | |||
|rowspan=2| <code><</code> | |||
| <code>≥</code> | |||
| <code>≤</code> | |||
|- | |||
| <code>/=</code> | |||
| <code>>=</code> | |||
| <code><=</code> | |||
|- | |||
| <code>'''eq'''</code> | |||
| <code>'''ne'''</code> | |||
| <code>'''gt'''</code> | |||
| <code>'''lt'''</code> | |||
| <code>'''ge'''</code> | |||
| <code>'''le'''</code> | |||
|- | |||
! [[BASIC|BASIC-like]]<ref group="note">Including [[Visual Basic .NET]], [[OCaml]], [[SQL]], [[Standard ML]], and others.</ref> | |||
| <code>=</code> | |||
| <code><></code> | |||
| <code>></code> | |||
| <code><</code> | |||
| <code>>=</code> | |||
| <code><=</code> | |||
|- | |||
! [[MUMPS]] | |||
| <code>=</code> | |||
| <code>'=</code> | |||
| <code>></code> | |||
| <code><</code> | |||
| <code>'<</code> | |||
| <code>'></code> | |||
|- | |||
! [[Pascal (programming language)|Pascal-like]]<ref group="note">Including [[Simula]], [[Modula2]], [[Delphi]], [[Ada (programming language)|Ada]], [[Oberon]], [[OCaml]], [[Standard ML]], and others.</ref> | |||
| <code>=</code> | |||
| <code><></code> | |||
| <code>></code> | |||
| <code><</code> | |||
| <code>>=</code> | |||
| <code><=</code> | |||
|- | |||
! [[C (programming language)|C-like]]<ref group="note">Including [[C (programming language)|C]], [[C++]], [[C Sharp (programming language)|C#]], [[Go (programming language)|Go]], [[Java (programming language)|Java]], [[JavaScript]], [[Perl]] (numerical comparison only), [[PHP]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], and [[R (programming language)|R]].</ref> | |||
| <code>==</code> | |||
| <code>!=</code> | |||
| <code>></code> | |||
| <code><</code> | |||
| <code>>=</code> | |||
| <code><=</code> | |||
|- | |||
! [[Unix shell#Bourne shell compatible|Bourne-like]] [[Shell (computing)|shells]] <ref group="note">Including [[Bourne shell]], [[Bash (Unix shell)|Bash]], [[Korn shell]], and [[Windows PowerShell]]. The symbols <code><</code> and <code>></code> are usually used in a shell for [[Redirection (computing)|redirection]], so other symbols need to be used. Without the hyphen, is used in [[Perl]] for string comparison.</ref> | |||
| <code>-eq</code> | |||
| <code>-ne</code> | |||
| <code>-gt</code> | |||
| <code>-lt</code> | |||
| <code>-ge</code> | |||
| <code>-le</code> | |||
|- | |||
! [[Batch file]] | |||
| <code>EQU</code> | |||
| <code>NEQ</code> | |||
| <code>GTR</code> | |||
| <code>LSS</code> | |||
| <code>GEQ</code> | |||
| <code>LEQ</code> | |||
|- | |||
! rowspan="2" |[[MATLAB]]<ref group="note">MATLAB, although in other respects using similar syntax as C, does not use <code>!=</code>, as <code>!</code> in MATLAB sends the following text as a command line to the [[operating system]]. The first form is also used in [[Smalltalk]], with the exception of equality, which is <code>=</code>.</ref> | |||
| <code>==</code> | |||
| <code>~=</code> | |||
| <code>></code> | |||
| <code><</code> | |||
| <code>>=</code> | |||
| <code><=</code> | |||
|- | |||
| <code>eq(x,y)</code> | |||
| <code>ne(x,y)</code> | |||
| <code>gt(x,y)</code> | |||
| <code>lt(x,y)</code> | |||
| <code>ge(x,y)</code> | |||
| <code>le(x,y)</code> | |||
|- | |||
! rowspan="2" |[[Mathematica]]<ref>[http://reference.wolfram.com/mathematica/tutorial/RelationalAndLogicalOperators.html Relational and Logical Operators] of [[Mathematica]]</ref> | |||
| <code>==</code> | |||
| <code>!=</code> | |||
| <code>></code> | |||
| <code><</code> | |||
| <code>>=</code> | |||
| <code><=</code> | |||
|- | |||
| <code>Equal[x,y]</code> | |||
| <code>Unequal[x,y]</code> | |||
| <code>Greater[x,y]</code> | |||
| <code>Less[x,y]</code> | |||
| <code>GreaterEqual[x,y]</code> | |||
| <code>LessEqual[x,y]</code> | |||
|} | |||
{{Reflist|group="note"}} | |||
Other conventions are less common: [[Common Lisp]] and [[Macsyma]]/[[Maxima (software)|Maxima]] use Basic-like operators except for inequality, which is /= in Common Lisp and # in Macsyma/Maxima. Older [[Lisp]]s used <code>equal</code>, <code>greaterp</code>, and <code>lessp</code>; and negated them using <code>not</code> for the remaining operators. | |||
==Equality== | |||
=== Confusion with assignment operators === | |||
Early FORTRAN (1956–57) was bounded by heavily restricted character sets where "<code>=</code>" was the only relational operator available. There were no "<" or ">" (and certainly no ≤ or ≥). This forced the designers to define symbols such as .GT., .LT., .GE., .EQ. etc. and subsequently made it tempting to use the remaining "<code>=</code>" character for copying, despite the obvious incoherence with mathematical usage (X=X+1 should be impossible). | |||
[[International Algebraic Language]] and [[ALGOL (programming language)|ALGOL]] (1958 and 1960) therefore introduced "<code>:=</code>" for assignment, leaving the standard "<code>=</code>" available for equality, a convention followed by [[CPL (programming language)|CPL]], [[Algol W]], [[BCPL]], [[Simula]], [[Algol 68]], [[SETL]], [[Pascal (programming language)|Pascal]], [[Smalltalk]], [[Modula2]], [[Ada (programming language)|Ada]], [[Standard ML]], [[OCaml]], [[Eiffel (programming language)|Eiffel]], [[Object Pascal|Delphi]], [[Oberon]], [[Dylan (programming language)|Dylan]], [[VHDL]], and several other languages. | |||
On the other hand, the now very influential language [[C (programming language)|C]] started off as a minimal compiled language called [[B (programming language)|B]], which, in turn, started off as a simplified version of [[BCPL]] (a typeless version of [[CPL (programming language)|CPL]]). The intented application for B was solely as a vehicle for a first port of (a then very primitive) UNIX. In what that has been described as a "strip-down" process, B replaced the original "<code>:=</code>" and "<code>=</code>" of BCPL by "<code>=</code>" and "<code>==</code>" respectively, the reason for this being unknown ('''and''' and '''or''' meanwhile became "<code>&</code>" and "<code>|</code>", and later "<code>&&</code>" and "<code>||</code>", respectively). As a small type system was later introduced, B became C. The popularity of C, and its association with UNIX, led to Java, C#, and other languages (including new versions of Fortran) following suit, syntactically, despite this unnecessary conflict with the mathematical meaning of the equal sign. | |||
====Languages==== | |||
Assignments in C have a [[value (programming)|value]] and since any non-zero scalar value is interpreted as ''true'' in [[Conditional (programming)|conditional expression]]s,<ref>A zero scalar value is interpreted as false while any non-zero scalar value is interpreted as true; this is typically used with integer types, similar to [[assembly language]] idioms.</ref> the code "<code>if (x = y)</code>" is legal, but has a very different meaning from "<code>if (x == y)</code>". The former code fragment means "assign ''y'' to ''x'', and if the new value of ''x'' is not zero, execute the following statement". The latter fragment means "[[if and only if]] ''x'' is equal to ''y'', execute the following statement".<ref name="kandr">{{cite book | title=The C Programming Language | last=Kernighan | first=Brian | coauthors=Dennis Ritchie | publisher=Prentice Hall | origyear=1978 | year=1988 | edition=Second edition}}, 19</ref> | |||
<source lang="c"> | |||
int x = 1; | |||
int y = 2; | |||
if (x = y) { | |||
/* This code will always execute if y is anything but 0*/ | |||
printf("x is %d and y is %d\n", x, y); | |||
} | |||
</source> | |||
Though [[Java (programming language)|Java]] and [[C Sharp (programming language)|C#]] have the same operators as C, this mistake usually causes a compile error in these languages instead, because the if-condition must be of type <tt>boolean</tt>, and there is no implicit way to convert from other types (e.g. numbers) into <tt>boolean</tt>s. So unless the variable that is assigned to has type <tt>boolean</tt> (or wrapper type <tt>Boolean</tt>), there will be a compile error. | |||
In Algol-like languages such as Pascal, Delphi and Ada (in the sense that they allow [[nested function definition]]s) as well as in [[Python (programming language)|Python]] and many functional languages, among others, assignment operators cannot appear in an [[expression (programming)|expression]] (including <code>if</code> clauses), thus precluding this class of error. Some compilers, such as [[GNU Compiler Collection|GCC]], will provide a warning when compiling code that contains an assignment operator inside an if statement, though there are some legitimate uses of an assignment inside an if-condition. In those cases the programmer would need to explicitly wrap the assignment in an extra pair of parentheses to avoid the warning. | |||
Similarly, some languages, such as [[BASIC]] use just the "<code>=</code>" symbol for both assignment ''and'' equality, as they are syntactically separate (as with Pascal, Ada, Python, etc., assignment operators cannot appear in expressions). | |||
Some programmers get in the habit of writing comparisons against a constant in the reverse of the usual order: | |||
<source lang="c"> | |||
if (2 == a) { /* Mistaken use of = versus == would be a compile-time error */ | |||
} | |||
</source> | |||
If the programmer accidentally uses <code>=</code>, the resulting code is invalid because 2 is not a variable. The compiler will generate an error message, upon which the proper operator can be substituted. This coding style is known as [[Coding conventions#Left-hand comparisons|left-hand comparison]]. | |||
=== Object identity vs. Content equality === | |||
In many modern programming languages, objects and data structures are accessed through [[Reference (computer science)|references]]. In such languages, there becomes a need to test for two different types of equality: | |||
* Physical (or shallow) equality - whether two references reference the same object. | |||
* Structural (or deep) equality - whether the objects referenced by two references are equivalent in some sense (e.g. their contents are the same). | |||
The first type of equality usually implies the second (except for things like [[NaN]] which are unequal to themselves), but the converse is not necessarily true. For example, two [[String (computer science)|string]] objects may be distinct objects (unequal in the first sense) but contain the same sequence of characters (equal in the second sense). See [[identity (object-oriented programming)|identity]] for more of this issue. | |||
The following table lists the different mechanisms to test for these two types of equality in various languages: | |||
{| class="wikitable" | |||
! Language !! Physical equality !! Structural equality !! Notes | |||
|- | |||
| [[ALGOL 68]] || <code>a :=: b</code> ''or'' <code>a '''is''' b</code> || <code>a = b</code> || when <code>a</code> and <code>b</code> are pointers | |||
|- | |||
| [[C (programming language)|C]], [[C++]] || <code>a == b</code> || <code>*a == *b</code> || when <code>a</code> and <code>b</code> are pointers | |||
|- | |||
| [[C Sharp (programming language)|C#]] || <code>object.ReferenceEquals(a, b)</code> || <code>a.Equals(b)</code> || The <code>==</code> operator defaults to <code>ReferenceEquals</code>, but can be [[Operator overloading|overloaded]] to perform <code>Equals</code> instead. | |||
|- | |||
| [[Common Lisp]] || <code>(eq a b)</code> || <code>(equal a b)</code> || | |||
|- | |||
| [[Go (programming language)|Go]] || <code>a == b</code> || <code>reflect.DeepEqual(*a, *b)</code> || when a and b are pointers | |||
|- | |||
| [[Java (programming language)|Java]] || <code>a == b</code> || <code>a.equals(b)</code> || | |||
|- | |||
| [[JavaScript]] || <code>a === b</code> || <code>a == b</code> || when a and b are two string objects that contain equivalent characters, the === operator will still return true. || | |||
|- | |||
| [[OCaml]], [[Smalltalk]] || <code>a == b</code> || <code>a = b</code> || | |||
|- | |||
| [[Pascal (programming language)|Pascal]] || <code>a^ = b^</code> ||<code>a = b</code> || | |||
|- | |||
| [[Perl]] || <code>$a == $b</code> || <code>$$a == $$b</code> || when <code>$a</code> and <code>$b</code> are references to scalars | |||
|- | |||
| [[PHP]]5 || <code>$a === $b</code> || <code>$a == $b</code> || when <code>$a</code> and <code>$b</code> are objects | |||
|- | |||
| [[Python (programming language)|Python]] || <code>a is b</code> || <code>a == b</code> || | |||
|- | |||
| [[Ruby (programming language)|Ruby]] || <code>a.equal?(b)</code> || <code>a == b</code> || | |||
|- | |||
| [[Scheme (programming language)|Scheme]] || <code>(eq? a b)</code> || <code>(equal? a b)</code> || | |||
|- | |||
| [[Visual Basic .NET]] <ref group="inequality">'''Patent application:''' On 14 May 2003, {{US patent application|20040230959}} "IS NOT OPERATOR" was filed for the '''<code>ISNOT</code>''' operator by employees of [[Microsoft]]. This patent was granted on 18 November 2004.</ref>|| <code>a Is b</code> or <code>object.ReferenceEquals(a, b)</code> || <code>a = b</code> or <code>a.Equals(b)</code> || Same as C#. | |||
|- | |||
| [[Objective-C]] ([[Cocoa (API)|Cocoa]], [[GNUstep]]) || <code>a == b</code> || <code>[a isEqual:b]</code> || when <code>a</code> and <code>b</code> are pointers to objects that are instances of <code>NSObject</code> | |||
|} | |||
{{Reflist|group="inequality"}} | |||
===The <nowiki>===</nowiki> operator === | |||
The languages [[JavaScript]] and [[PHP]] extend this syntax, with the "<code>==</code>" operator able to return true if two values are equal, even if they have different types (for example, "<code>4 == "4"</code>" is true), and the "<code>===</code>" operator returning true only if two values are equal and have equivalent types as well (such that "<code>4 === "4"</code>" is false but "<code>4 === 4</code>" is true).<ref name="php">{{cite web|url=http://php.net/manual/en/language.operators.comparison.php |title=PHP: Comparison Operators - Manual|accessdate=2008-07-31}}</ref> This comes in handy when checking if a value is assigned the value of 0, since "<code>x == 0</code>" is true for x being <code>0</code>, but also for x being <code>"0"</code> (i.e. a string containing the character 0) and <code>false</code> (as PHP, like other languages, equates <code>0</code> to <code>false</code>), and that is not always what one wants,<ref name="php" /> but "<code>x === 0</code>" is only true when x is <code>0</code>. When comparing objects in PHP 5, the "<code>==</code>" operator tests for structural equality, while the "<code>===</code>" operator tests for physical equality.<ref>http://www.php.net/manual/en/language.oop5.object-comparison.php</ref> | |||
==Logical equivalence== | |||
Though perhaps not obvious at first, like the [[Boolean logic|boolean]] [[logical operator]]s 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'' (either all true or all false) for any given ''x'' and ''y'' values: | |||
:<math> | |||
E = \begin{cases} | |||
x < y \\ | |||
y > x \\ | |||
x \ngeq y \\ | |||
y \nleq x | |||
\end{cases}</math> | |||
==See also== | |||
* [[Binary relation]] | |||
* [[Common operator notation]] | |||
* [[Equality (mathematics)]] | |||
* [[Equals sign]] | |||
* [[Logical operator]] | |||
* [[Operation (mathematics)]] | |||
* [[Operator (mathematics)]] | |||
* [[Operator (programming)]] | |||
* [[Spaceship operator]] | |||
* [[Triadic relation]] | |||
==Notes and references== | |||
{{reflist}} | |||
{{DEFAULTSORT:Relational Operator}} | |||
[[Category:Operators (programming)]] |
Revision as of 12:22, 30 January 2014
Template:Lead too long In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3).
In programming languages that include a distinct boolean data type in their type system, like Java, these operators return true or false, depending on whether the conditional relationship between the two operands holds or not. In other languages such as C, relational operators return the integers 0 or 1. Some programming languages make a syntactical distinction between the "equals" of assignment (e.g. a = 1
assigns the value 1 to the variable "a") and the "equals" of comparison (if a == 1 then ...
). Other languages determine which is meant from context.
"Greater than" and "less than" comparison of non-numeric data is performed according to a sort convention (such as, for text strings, lexicographical order) which may be built into the programming language and/or configurable by the 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.
In floating-point arithmetic, numbers, including many simple fractions, cannot be represented exactly, and it may be necessary to test for equality within a given tolerance.
Comparison of programmer-defined data types (data types of which the programming language itself has no in-built understanding) may be carried out by custom-written or library functions (such as strcmp
mentioned above), or, in some languages, by "overloading" a comparison operator – that is, assigning a programmer-defined meaning that depends on the data types being compared.
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 objects with different datatypes both related to another datatype, e.g. an orange and a lemon, both being citrus fruit
- two different objects of the same type, e.g. two hands
- two objects being equal but distinct, e.g. two $10 banknotes
- two different references to the same object, e.g. two nicknames for the same person
Sameness and difference can be relative or graduated as well as absolute, particularly in fuzzy logic, artificial intelligence, signal processing, lossy data compression and pattern recognition.
An expression created using a relational operator forms what is known as a relational expression or a condition. Relational operators are also used in technical literature instead of words. Relational operators are usually written in infix notation, if supported by the programming language, which means that they appear between their operands (the two expressions being related). For example, an expression in C will print the message if the x is less than y:
if (x < y) {
printf("x is less than y in this example\n");
}
Other programming languages, such as Lisp, use prefix notation, as follows:
(>= X Y)
Standard relational operators
The most common numerical relational operators used in programming languages are shown below.
Convention | equal to | not equal to | greater than | less than | greater than or equal to |
less than or equal to |
---|---|---|---|---|---|---|
In print | = | ≠ | > | < | ≥ | ≤ |
Fortran [note 1] | .EQ.
|
.NE.
|
.GT.
|
.LT.
|
.GE.
|
.LE.
|
ALGOL 68 [note 2] | =
|
≠
|
>
|
<
|
≥
|
≤
|
/=
|
>=
|
<=
| ||||
eq
|
ne
|
gt
|
lt
|
ge
|
le
| |
BASIC-like[note 3] | =
|
<>
|
>
|
<
|
>=
|
<=
|
MUMPS | =
|
'=
|
>
|
<
|
'<
|
'>
|
Pascal-like[note 4] | =
|
<>
|
>
|
<
|
>=
|
<=
|
C-like[note 5] | ==
|
!=
|
>
|
<
|
>=
|
<=
|
Bourne-like shells [note 6] | -eq
|
-ne
|
-gt
|
-lt
|
-ge
|
-le
|
Batch file | EQU
|
NEQ
|
GTR
|
LSS
|
GEQ
|
LEQ
|
MATLAB[note 7] | ==
|
~=
|
>
|
<
|
>=
|
<=
|
eq(x,y)
|
ne(x,y)
|
gt(x,y)
|
lt(x,y)
|
ge(x,y)
|
le(x,y)
| |
Mathematica[1] | ==
|
!=
|
>
|
<
|
>=
|
<=
|
Equal[x,y]
|
Unequal[x,y]
|
Greater[x,y]
|
Less[x,y]
|
GreaterEqual[x,y]
|
LessEqual[x,y]
|
43 year old Petroleum Engineer Harry from Deep River, usually spends time with hobbies and interests like renting movies, property developers in singapore new condominium and vehicle racing. Constantly enjoys going to destinations like Camino Real de Tierra Adentro.
Other conventions are less common: Common Lisp and Macsyma/Maxima use Basic-like operators except for inequality, which is /= in Common Lisp and # in Macsyma/Maxima. Older Lisps used equal
, greaterp
, and lessp
; and negated them using not
for the remaining operators.
Equality
Confusion with assignment operators
Early FORTRAN (1956–57) was bounded by heavily restricted character sets where "=
" was the only relational operator available. There were no "<" or ">" (and certainly no ≤ or ≥). This forced the designers to define symbols such as .GT., .LT., .GE., .EQ. etc. and subsequently made it tempting to use the remaining "=
" character for copying, despite the obvious incoherence with mathematical usage (X=X+1 should be impossible).
International Algebraic Language and ALGOL (1958 and 1960) therefore introduced ":=
" for assignment, leaving the standard "=
" available for equality, a convention followed by CPL, Algol W, BCPL, Simula, Algol 68, SETL, Pascal, Smalltalk, Modula2, Ada, Standard ML, OCaml, Eiffel, Delphi, Oberon, Dylan, VHDL, and several other languages.
On the other hand, the now very influential language C started off as a minimal compiled language called B, which, in turn, started off as a simplified version of BCPL (a typeless version of CPL). The intented application for B was solely as a vehicle for a first port of (a then very primitive) UNIX. In what that has been described as a "strip-down" process, B replaced the original ":=
" and "=
" of BCPL by "=
" and "==
" respectively, the reason for this being unknown (and and or meanwhile became "&
" and "|
", and later "&&
" and "||
", respectively). As a small type system was later introduced, B became C. The popularity of C, and its association with UNIX, led to Java, C#, and other languages (including new versions of Fortran) following suit, syntactically, despite this unnecessary conflict with the mathematical meaning of the equal sign.
Languages
Assignments in C have a value and since any non-zero scalar value is interpreted as true in conditional expressions,[2] the code "if (x = y)
" is legal, but has a very different meaning from "if (x == y)
". The former code fragment means "assign y to x, and if the new value of x is not zero, execute the following statement". The latter fragment means "if and only if x is equal to y, execute the following statement".[3]
int x = 1;
int y = 2;
if (x = y) {
/* This code will always execute if y is anything but 0*/
printf("x is %d and y is %d\n", x, y);
}
Though Java and C# have the same operators as C, this mistake usually causes a compile error in these languages instead, because the if-condition must be of type boolean, and there is no implicit way to convert from other types (e.g. numbers) into booleans. So unless the variable that is assigned to has type boolean (or wrapper type Boolean), there will be a compile error.
In Algol-like languages such as Pascal, Delphi and Ada (in the sense that they allow nested function definitions) as well as in Python and many functional languages, among others, assignment operators cannot appear in an expression (including if
clauses), thus precluding this class of error. Some compilers, such as GCC, will provide a warning when compiling code that contains an assignment operator inside an if statement, though there are some legitimate uses of an assignment inside an if-condition. In those cases the programmer would need to explicitly wrap the assignment in an extra pair of parentheses to avoid the warning.
Similarly, some languages, such as BASIC use just the "=
" symbol for both assignment and equality, as they are syntactically separate (as with Pascal, Ada, Python, etc., assignment operators cannot appear in expressions).
Some programmers get in the habit of writing comparisons against a constant in the reverse of the usual order:
if (2 == a) { /* Mistaken use of = versus == would be a compile-time error */
}
If the programmer accidentally uses =
, the resulting code is invalid because 2 is not a variable. The compiler will generate an error message, upon which the proper operator can be substituted. This coding style is known as left-hand comparison.
Object identity vs. Content equality
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:
- Physical (or shallow) equality - whether two references reference the same object.
- Structural (or deep) equality - whether the objects referenced by two references are equivalent in some sense (e.g. their contents are the same).
The first type of equality usually implies the second (except for things like NaN which are unequal to themselves), but the converse is not necessarily true. For example, two string objects may be distinct objects (unequal in the first sense) but contain the same sequence of characters (equal in the second sense). See identity for more of this issue.
The following table lists the different mechanisms to test for these two types of equality in various languages:
Language | Physical equality | Structural equality | Notes | |
---|---|---|---|---|
ALGOL 68 | a :=: b or a is b |
a = b |
when a and b are pointers
| |
C, C++ | a == b |
*a == *b |
when a and b are pointers
| |
C# | object.ReferenceEquals(a, b) |
a.Equals(b) |
The == operator defaults to ReferenceEquals , but can be overloaded to perform Equals instead.
| |
Common Lisp | (eq a b) |
(equal a b) |
||
Go | a == b |
reflect.DeepEqual(*a, *b) |
when a and b are pointers | |
Java | a == b |
a.equals(b) |
||
JavaScript | a === b |
a == b |
when a and b are two string objects that contain equivalent characters, the === operator will still return true. | |
OCaml, Smalltalk | a == b |
a = b |
||
Pascal | a^ = b^ |
a = b |
||
Perl | $a == $b |
$$a == $$b |
when $a and $b are references to scalars
| |
PHP5 | $a === $b |
$a == $b |
when $a and $b are objects
| |
Python | a is b |
a == b |
||
Ruby | a.equal?(b) |
a == b |
||
Scheme | (eq? a b) |
(equal? a b) |
||
Visual Basic .NET [inequality 1] | a Is b or object.ReferenceEquals(a, b) |
a = b or a.Equals(b) |
Same as C#. | |
Objective-C (Cocoa, GNUstep) | a == b |
[a isEqual:b] |
when a and b are pointers to objects that are instances of NSObject
|
43 year old Petroleum Engineer Harry from Deep River, usually spends time with hobbies and interests like renting movies, property developers in singapore new condominium and vehicle racing. Constantly enjoys going to destinations like Camino Real de Tierra Adentro.
The === operator
The languages JavaScript and PHP extend this syntax, with the "==
" operator able to return true if two values are equal, even if they have different types (for example, "4 == "4"
" is true), and the "===
" operator returning true only if two values are equal and have equivalent types as well (such that "4 === "4"
" is false but "4 === 4
" is true).[4] This comes in handy when checking if a value is assigned the value of 0, since "x == 0
" is true for x being 0
, but also for x being "0"
(i.e. a string containing the character 0) and false
(as PHP, like other languages, equates 0
to false
), and that is not always what one wants,[4] but "x === 0
" is only true when x is 0
. When comparing objects in PHP 5, the "==
" operator tests for structural equality, while the "===
" operator tests for physical equality.[5]
Logical equivalence
Though perhaps not obvious 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 (either all true or all false) for any given x and y values:
See also
- Binary relation
- Common operator notation
- Equality (mathematics)
- Equals sign
- Logical operator
- Operation (mathematics)
- Operator (mathematics)
- Operator (programming)
- Spaceship operator
- Triadic relation
Notes and references
43 year old Petroleum Engineer Harry from Deep River, usually spends time with hobbies and interests like renting movies, property developers in singapore new condominium and vehicle racing. Constantly enjoys going to destinations like Camino Real de Tierra Adentro.
Cite error: <ref>
tags exist for a group named "note", but no corresponding <references group="note"/>
tag was found
- ↑ Relational and Logical Operators of Mathematica
- ↑ A zero scalar value is interpreted as false while any non-zero scalar value is interpreted as true; this is typically used with integer types, similar to assembly language idioms.
- ↑ 20 year-old Real Estate Agent Rusty from Saint-Paul, has hobbies and interests which includes monopoly, property developers in singapore and poker. Will soon undertake a contiki trip that may include going to the Lower Valley of the Omo.
My blog: http://www.primaboinca.com/view_profile.php?userid=5889534, 19 - ↑ 4.0 4.1 Template:Cite web
- ↑ http://www.php.net/manual/en/language.oop5.object-comparison.php
Cite error: <ref>
tags exist for a group named "inequality", but no corresponding <references group="inequality"/>
tag was found