Equality and the Erlang standard library
Erlang’s notion of equality can be confusing, especially when dealing with numeric types. Recall the Erlang term order:
number < atom < reference < fun < port < pid < tuple < list < binary- Lists are compared element by element.
- Tuples are ordered by size, two tuples with the same size are compared element by element.
Most operators promote integer operands to floats.
|-----|----------|
| op | 1 op 1.0 |
|-----|----------|
| == | true |
| =:= | false |
| /= | false |
| =/= | true |
|-----|----------|
=< and >= use ==-equality. We have:
Eshell V5.7.4 (abort with ^G)
1> [1, 2] < [2].
true
2> {1.0} =< {1}.
trueParticular care must be taken when using numbers as part of the key in one of the standard collections. Trying to look up the result of an arithmetic expression involving a float in a collection which uses =:= internally often annoys.
|--------------|-----|
| module | op |
|--------------|-----|
| dict | =:= |
| orddict | == |
| proplists | =:= |
| gb_trees | == |
| sets | =:= |
| ordsets | == |
| gb_sets | == |
| lists:key* | == |
| lists:ukey* | == |
| lists:delete | =:= |
| lists:member | =:= |
|--------------|-----|
C.f. this recent thread on the erlang-questions list.