What is the difference between equality and equivalence?

后端 未结 9 757
难免孤独
难免孤独 2020-12-13 07:08

I\'ve read a few instances in reading mathematics and computer science that use the equivalence symbol , (basically an \'=\' with three lines)

9条回答
  •  隐瞒了意图╮
    2020-12-13 07:16

    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.

提交回复
热议问题