Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Comparison Operators

The comparison operators compare two values and yield a bool.

  • == equality
  • != inequality
  • < less than
  • <= less than or equal
  • > greater than
  • >= greater than or equal

Process

At runtime:

  1. the left operand is evaluated;
  2. the right operand is evaluated; and
  3. the operator is applied to both values, and its result is the expression's value.

The ordering operators <, <=, >, and >= compare their operands numerically; for floating-point operands, comparisons follow IEEE 754 semantics.

The equality operators == and != apply equality: two values are equal when they have the same type and

  • for numeric types, their values are numerically equal; for integer types this means the same bit pattern, and for floating-point types comparisons follow IEEE 754 semantics;
  • for bool, they are the same state;
  • for handles, their handle values are equal: both point to the same region-allocated storage;
  • for structures, their fields are equal in order;
  • for tuples, their elements are equal in order;
  • for enumerations, they denote the same variant and their payloads are equal; and
  • for slices, their representations are equal: the same length and the same address of the first element.

Slice equality is identity-based: a slice's representation (length and first-element address) is compared, not its elements.

Assertions

  1. The operands have a common type, to which each operand is implicitly converted for comparison.
  2. The ordering operators <, <=, >, and >= require numeric operands within the same family: signed integer, unsigned integer, or floating-point.
  3. The equality operators == and != require that, once converted to their common type, the operands are numeric types within the same family, or identical types among bool, handle, structure, tuple, enumeration, and slice.

Syntax

comparison_operator = equal_equal | bang_equal | less_than | less_than_equal
                    | greater_than | greater_than_equal ;