1
$\begingroup$

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

In[1]:= ep = 2.5*^-14;

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

In[3]:= E == E + 2 ep   (* More than 7 *)
Out[3]= 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[4]:= E + ep == E + 2 ep
Out[4]= True

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

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

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

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

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

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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[1]:= a == 2 == 3   (* Expect False *)
Out[1]= a == 2 == 3

In[2]:= 2 == 3 == a
Out[2]= 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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Jul 1 '16 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.