# Chains of approximate numeric equalities

Mathematica considers two numbers equal if "at most their last seven binary digits" differ. For example:

In:= ep = 2.5*^-14;

In:= E == E + ep     (* Less than 7 binary digits *)
Out= True

In:= E == E + 2 ep   (* More than 7 *)
Out= False


This seems strange but I understand the motivation. I then wondered if it's possible to construct a chain of equalities with unequal ends but nevertheless evaluates to True. It turns out no:

In:= E + ep == E + 2 ep
Out= True

In:= E == E + ep == E + 2 ep   (* First and last argument differ *)
Out= False


However, if you use LessEqual (which behaves similarly to Equal) it is possible:

In:= E + 2 ep <= E + ep <= E  (* Expected False *)
Out= True

In:= E + 2 ep <= E            (* As in Equal *)
Out= False


My question is, is there a reason for this odd behaviour or is it just a quirk to be aware of?

It's all explained in the Documentation Center.

Equal

Subscript[e, 1] == Subscript[e, 2] == Subscript[e, 3] gives True if all the Subscript[e, i] are equal.

LessEqual

Subscript[x, 1] <= Subscript[x, 2] <= Subscript[x, 3] yields True if the Subscript[x, i] form a nondecreasing sequence.

That is, the three arguments in Equal must be equal in all possible pairings; the three arguments in LessEqual must satisfy the indicated relation only as adjacent pairs; i.e., in a chain.

Another way of thinking about this difference in behavior is that Equal is reflexive, commutative, and transitive, while LessEqual is only reflexive and transitive. (And Less is only transitive.) Being commutative requires the checking of all pairings.

Adding this as an answer since it's too long for a comment.

m_goldberg suggests that all pairs must be compared but that's not the full story, consider

In:= a == 2 == 3   (* Expect False *)
Out= a == 2 == 3

In:= 2 == 3 == a
Out= False


Doing some experiments it appears that all pairs of arguments are compared in lexicographic order; if two elements are pairwise unequal then return False, if they're not comparable return the expression unevaluated, otherwise return True.

Whether an expression is False or unevaluated depends on whether an unequal or incomparable pair is encountered first.

• What you discuss in this answer is the behavior of all Mathematica relational operators. It can be summed up by saying they are not predicates. – m_goldberg Jul 1 '16 at 10:55