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!
===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.
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)