Does “<” satisfies strict weak ordering means that there's no need for “==” definition?

  • A+
Category:Languages

I don't know much about the math behind strict weak ordering. But I've read something:

An operator that satisfies strict weak ordering can express all other logical operators

<(a, b)  : (a < b) <=(a, b): !(b < a) ==(a, b): !(a < b) && !(b < a) !=(a, b) : (a < b) || (b < a) >(a, b)  : (b < a) >=(a, b): !(a < b) 

So does it means that there's no need for "==" definition, since "<" satisfies strict weak ordering, which can express all logical operators? (This may obviously seem not correct because I've seen classes define both < and ==.)

An easy to understand explanation would be also much-appreciated :D. Not necessary to be too "mathematical" or "technically right".


Could someone give me 1 or 2 (if they are easy to find) examples that the standard library defines other operators based on <? A reference to source code would be good enough.

 


First thing's first. Defining operator < doesn't mean you get a definition of operator == for free from the compiler. It still needs to be defined explicitly.

The table above assumes something that is true for many types and ordering relations, but not for all. It assumes that equivalence between two elements implies equality. That need not hold.

We can certainly use < to check for equivalence between two elements. They are equivalent according to the strict week ordering (that's what !(a < b) && !(b < a) means). It doesn't necessarily mean those elements are equal.

A good example would be case insensitive comparison between strings of characters. In that case we will certainly have !("ab" < "AB") && !("AB" < "ab"), and the two string are equivalent, but they aren't equal in value.

Having said all that. If the order relation you defined implies all the other ones for your type, there are tricks to generate all the other operations from it. Just as the table demonstrates.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: