8.11 Term Comparisons

  1. Expr1 op Expr2
opDescription
==Equal to
/=Not equal to
=<Less than or equal to
<Less than
>=Greater than or equal to
>Greater than
=:=Exactly equal to
=/=Exactly not equal to

Table 8.1: Term Comparison Operators.

The arguments can be of different data types. The following order is defined:

  1. number < atom < reference < fun < port < pid < tuple < map < nil < list < bit string

nil in the previous expression represents the empty list ([]), which is regarded as a separate type from list/0. That is why nil < list.

Lists are compared element by element. Tuples are ordered by size, two tuples with the same size are compared element by element.

Maps are ordered by size, two maps with the same size are compared by keys in ascending term order and then by values in key order. In maps key order integers types are considered less than floats types.

Atoms are compared using their string value, codepoint by codepoint.

When comparing an integer to a float, the term with the lesser precision is converted into the type of the other term, unless the operator is one of =:= or =/=. A float is more precise than an integer until all significant figures of the float are to the left of the decimal point. This happens when the float is larger/smaller than +/-9007199254740992.0. The conversion strategy is changed depending on the size of the float because otherwise comparison of large floats and integers would lose their transitivity.

Term comparison operators return the Boolean value of the expression, true or false.

Examples:

  1. 1> 1==1.0.
  2. true
  3. 2> 1=:=1.0.
  4. false
  5. 3> 1 > a.
  6. false
  7. 4> #{c => 3} > #{a => 1, b => 2}.
  8. false
  9. 4> #{a => 1, b => 2} == #{a => 1.0, b => 2.0}.
  10. true