-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9cb1e3
commit b5e0b73
Showing
1 changed file
with
20 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,34 +139,19 @@ All Objects | |
|
||
.. function:: isequal(x, y) | ||
|
||
True if and only if ``x`` and ``y`` would cause a typical function to behave the | ||
same. A "typical" function is one that uses only intended interfaces, and does not | ||
unreasonably exploit implementation details of its arguments. For example, | ||
floating-point ``NaN`` values are ``isequal`` regardless of their sign bits, since | ||
the sign of a ``NaN`` has no meaning in the vast majority of cases (but can be | ||
discovered if you really want to). | ||
|
||
One implication of this definition is that implementing ``isequal`` for a new type | ||
encapsulates, to a large extent, what the true abstraction presented by that type | ||
is. For example, a ``String`` is a sequence of characters, so two strings are | ||
``isequal`` if they generate the same characters. Other concerns, such as encoding, | ||
are not considered. | ||
|
||
When calling ``isequal``, be aware that it cannot be all things to all people. | ||
For example, if your use of strings *does* care about encoding, you will have to | ||
perform a check like ``typeof(x)==typeof(y) && isequal(x,y)``. | ||
|
||
``isequal`` is the default comparison function used by hash tables (``Dict``). | ||
``isequal(x,y)`` must imply ``hash(x)==hash(y)``. | ||
Similar to ``==``, except treats all floating-point ``NaN`` values as equal, and | ||
treats ``-0.0`` as unequal to ``0.0``. Falls back to ``==``. | ||
|
||
New types with a notion of equality should implement this function, except for | ||
numbers, which should implement ``==`` instead. However, numeric types with special | ||
values like ``NaN`` might need to implement ``isequal`` as well. Numbers of different | ||
types are considered unequal. | ||
``isequal`` is the comparison function used by hash tables (``Dict``). | ||
``isequal(x,y)`` must imply ``hash(x)==hash(y)``. | ||
|
||
Mutable containers should generally implement ``isequal`` by calling ``isequal`` | ||
recursively on all contents. | ||
|
||
Other new types generally do not need to implement this function, unless they | ||
represent floating-point numbers amenable to a more efficient implementation | ||
than that provided by a generic fallback (based on ``isnan``, ``signbit``, and ``==``). | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JeffBezanson
Author
Member
|
||
|
||
.. function:: isless(x, y) | ||
|
||
Test whether ``x`` is less than ``y``, according to a canonical total order. | ||
|
@@ -2288,8 +2273,18 @@ Mathematical Operators | |
.. _==: | ||
.. function:: ==(x, y) | ||
|
||
Numeric equality operator. Compares numbers and number-like values (e.g. arrays) by numeric value. True for numbers of different types that represent the same value (e.g. ``2`` and ``2.0``). Follows IEEE semantics for floating-point numbers. | ||
New numeric types should implement this function for two arguments of the new type. | ||
Generic equality operator, giving a single ``Bool`` result. Falls back to ``===``. | ||
Should be implemented for all types with a notion of equality, based | ||
on the abstract value that an instance represents. For example, all numeric types are compared | ||
by numeric value, ignoring type. Strings are compared as sequences of characters, ignoring | ||
encoding. | ||
|
||
Follows IEEE semantics for floating-point numbers. | ||
|
||
Mutable containers should generally implement ``==`` by calling ``==`` recursively on all contents. | ||
|
||
New numeric types should implement this function for two arguments of the new type, and handle | ||
comparison to other types via promotion rules where possible. | ||
|
||
.. _!=: | ||
.. function:: !=(x, y) | ||
|
This is unfortunately not true for collections since we just fall back on
==
which is incorrect in that case. This pull request doesn't attempt to fix that pre-existing situation, but I think that we really ought to fix this. The real fix may be to provide the generic ability to recursively compare two structures and define both==
andisequal
on collections in terms of that. Then we can let people override this when they have a new collection type.