EqualsEval

EqualsEval

expr1 == expr2 checks if two expressions are equal, and returns True or False.

Usages

expr1 == expr2

expr1 == expr2 (or EqualsEval[ expr1, expr2 ]) returns:

  • True if both expressions are the same, or have the same numerical value;
  • False if they are certainly different, for example two different numbers;
  • the comparison itself, unevaluated, if it cannot be decided, for example because the expressions contain variables.

It is typically used in conditions, such as in If and While.

Example in out
Two equal numbers. CalcPlus[1,1] == 2 True
Expressions with the same numerical value. 1+1 == 2 True
A fraction and a decimal number with the same value. 1/2 == 0.5 True
Two different numbers. 2 == 3 False
The same expression, in a different order. x+1 == 1+x True
Cannot be decided, because it depends on x. x+2 == 2x x+2 == 2x
Test if a script variable is even. n := 12
r := Mod[n, 2] == 0
r = True
Options

CompareLiteral

If set to True, the expressions are compared as they are written, without calculating their numerical values. The order of terms and factors still does not matter. This option requires the EqualsEval[ ... ] notation.

Syntax Example out
EqualsEval[ expr1, expr2, CompareLiteral=>True ] EqualsEval[ 1/2, 0.5, CompareLiteral=>True ] False
EqualsEval[ x+1, 1+x, CompareLiteral=>True ] True
Details
  • Numbers are compared by value: 2 == 2.0, 2/4 == 1/2 and Sqrt[4] == 2 are all True. Pi == 3.14 is False.
  • Lists: lists with the same elements in the same order are equal. Lists of different lengths are not equal: {1,2} == {1,2,3} is False. Lists with the same elements in a different order are not decided: {1,2} == {2,1} stays unevaluated.
  • NaN is not equal to anything: NaN == 1 is False.

See also: UnequalsEval, IsMatch, If, Smaller