Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Relational operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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 (computer science)|set]], or to access to a value through a key. It is used in [[switch statement]]s 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 type]]s and [[inheritance (object-oriented programming)|inheritance]], [[equality (mathematics)|equality]], and [[identity (mathematics)|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 [[Reference (computer science)|references]]. In such languages, there becomes a need to test for two different types of equality: * Location equality (identity): if two references (A and B) 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 (A and B) are equivalent in some sense: :* Structural equality (that is, their contents are the same). which may be either shallow (testing only immediate subparts), or deep (testing for equality of subparts recursively). A simple way to achieve this is through representational equality: checking that the values have the same representation. :* Some other tailor-made equality, preserving the external behavior. For example, 1/2 and 2/4 are considered equal when seen as a rational number. A possible requirement would be that "A = B if and only if all operations on objects A and B will have the same result", in addition to [[reflexive relation|reflexivity]], [[symmetry]], and [[transitive relation|transitivity]]. The first type of equality usually implies the second (except for things like ''not a number'' ([[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. Real numbers, including many simple [[fraction (mathematics)|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 754|IEEE floating-point]] standard requires that ''NaN β NaN'' holds. In contrast, the (2022) [[technical standard|private standard]] for [[posit (number format)|posit]] arithmetic (posit proponents mean to replace IEEE floats) has a similar concept, NaR (Not a Real), where ''NaR = NaR'' holds.<ref>[https://posithub.org/docs/posit_standard-2.pdf Standard for Posit Arithmetic (2022)]</ref> 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 [[Type system#DYNAMIC|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 (i.e., <code>a == b</code> and <code>b == c</code>, but <code>a != c</code>), or make certain values be equal to their own negation.<ref>{{cite web |last1=Denys |first1=Dovhan |title=WTFJS |website=[[GitHub]] |url=https://github.com/denysdovhan/wtfjs#-is-equal- |access-date=25 July 2024}}</ref> A strict equality operator is also often available in those languages, returning true only for values with identical or equivalent types (in PHP, <code>4 === "4"</code> is false although <code>4 == "4"</code> is true).<ref>{{cite web|url=http://www.php.net/manual/en/language.oop5.object-comparison.php |title=Comparing Objects |work=PHP Manual |publisher=PHP Group |access-date=June 29, 2014 }}</ref><ref name="php">{{cite web|url=http://php.net/manual/en/language.operators.comparison.php |title=PHP: Comparison Operators - Manual|access-date=July 31, 2008}}</ref> For languages where the number 0 may be interpreted as ''false'', this operator may simplify things such as checking for zero (as <code>x == 0</code> would be true for x being either 0 or "0" using the type agnostic equality operator).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)