36
4

9 回答 9

19

Wikipedia: Equivalence relation:

In mathematics, an equivalence relation is a binary relation between two elements of a set which groups them together as being "equivalent" in some way. Let a, b, and c be arbitrary elements of some set X. Then "a ~ b" or "a ≡ b" denotes that a is equivalent to b.

An equivalence relation "~" is reflexive, symmetric, and transitive.

In other words, = is just an instance of equivalence relation.

Edit: This seemingly simple criteria of being reflexive, symmetric, and transitive are not always trivial. See Bloch's Effective Java 2nd ed p. 35 for example,

public final class CaseInsensitiveString {
...
    // broken
    @Override public boolean equals(Object o) {
        if (o instance of CaseInsensitiveString)
            return s.equalsIgnoreCase(
                ((CaseInsensitiveString) o).s);
        if (o instanceof String) // One-way interoperability!
            return s.equalsIgnoreCase((String) o);
        return false;
    }
... 

}

The above equals implementation breaks the symmetry because CaseInsensitiveString knows about String class, but the String class doesn't know about CaseInsensitiveString.

于 2008-11-23T00:17:53.607 回答
17

I take your question to be about math notation rather than programming. The triple equal sign you refer to can be written ≡ in HTML or \equiv in LaTeX.

a ≡ b most commonly means "a is defined to be b" or "let a be equal to b".

So 2+2=4 but φ ≡ (1+sqrt(5))/2.

Here's a handy equivalence table:

Mathematicians      Computer scientists
--------------      -------------------
      =                      ==
      ≡                      =

(The other answers about equivalence relations are correct too but I don't think those are as common. There's also a ≡ b (mod m) which is pronounced "a is congruent to b, mod m" and in programmer parlance would be expressed as mod(a,m) == mod(b,m). In other words, a and b are equal after mod'ing by m.)

于 2008-12-02T05:39:26.060 回答
13

A lot of languages distinguish between equality of the objects and equality of the values of those objects.

Ruby for example has 3 different ways to test equality. The first, equal?, compares two variables to see if they point to the same instance. This is equivalent in a C-style language of doing a check to see if 2 pointers refer to the same address. The second method, ==, tests value equality. So 3 == 3.0 would be true in this case. The third, eql?, compares both value and class type.

Lisp also has different concepts of equality depending on what you're trying to test.

于 2008-11-23T00:29:22.430 回答
8

In languages that I have seen that differentiate between equality and equivalence, equality usually means the type and value are the same while equivalence means that just the values are the same. For example:

int i = 3;
double d = 3.0;

i and d would be have an equivalence relationship since they represent the same value but not equality since they have different types. Other languages may have different ideas of equivalence (such as whether two variables represent the same object).

于 2008-11-23T00:17:16.353 回答
6

The answers above are right / partially right but they don't explain what the difference is exactly. In theoretical computer science (and probably in other branches of maths) it has to do with quantification over free variables of the logical equation (that is when we use the two notations at once).

For me the best ways to understand the difference is:

  1. By definition
    A ≡ B
    means
    For all possible values of free variables in A and B, A = B

    or

    A ≡ B <=> [A = B]

  2. By example
    x=2x
    iff (in fact iff is the same as ≡)
    x=0

    x ≡ 2x
    iff (because it is not the case that x = 2x for all possible values of x)
    False

I hope it helps

Edit:

Another thing that came to my head is the definitions of the two.

A = B is defined as A <= B and A >= B, where <= (smaller equal, not implies) can be any ordering relation

A ≡ B is defined as A <=> B (iff, if and only if, implies both sides), worth noting that implication is also an ordering relation and so it is possible (but less precise and often confusing) to use = instead of ≡.

I guess the conclusion is that when you see =, then you have to figure out the authors intention based on the context.

于 2012-08-01T09:10:42.527 回答
4

Take it outside the realm of programming.

  • (31) equal -- (having the same quantity, value, or measure as another; "on equal terms"; "all men are equal before the law")

  • equivalent, tantamount -- (being essentially equal to something; "it was as good as gold"; "a wish that was equivalent to a command"; "his statement was tantamount to an admission of guilt"

At least in my dictionary, 'equivelance' means its a good-enough subsitute for the original, but not necessarily identical, and likewise 'equality' conveys complete identical.

null == 0   # true , null is equivelant to 0 ( in php ) 
null === 0  # false, null is not equal to 0 ( in php )  

( Some people use ≈ to represent nonidentical values instead )

于 2008-11-23T00:28:26.597 回答
4

The difference resides above all in the level at which the two concepts are introduced. '≡' is a symbol of formal logic where, given two propositions a and b, a ≡ b means (a => b AND b => a).

'=' is instead the typical example of an equivalence relation on a set, and presumes at least a theory of sets. When one defines a particular set, usually he provides it with a suitable notion of equality, which comes in the form of an equivalence relation and uses the symbol '='. For example, when you define the set Q of the rational numbers, you define equality a/b = c/d (where a/b and c/d are rational) if and only if ad = bc (where ad and bc are integers, the notion of equality for integers having already been defined elsewhere).

Sometimes you will find the informal notation f(x) ≡ g(x), where f and g are functions: It means that f and g have the same domain and that f(x) = g(x) for each x in such domain (this is again an equivalence relation). Finally, sometimes you find ≡ (or ~) as a generic symbol to denote an equivalence relation.

于 2008-11-23T01:30:44.503 回答
2

You could have two statements that have the same truth value (equivalent) or two statements that are the same (equality). As well the "equal sign with three bars" can also mean "is defined as."

于 2008-11-23T00:22:16.123 回答
2

Equality really is a special kind of equivalence relation, in fact. Consider what it means to say:

0.9999999999999999... = 1

That suggests that equality is just an equivalence relation on "string numbers" (which are defined more formally as functions from Z -> {0,...,9}). And we can see from this case, the equivalence classes are not even singletons.

于 2008-11-23T01:41:27.103 回答