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

Arithmetic Operators

The arithmetic operators compute a numeric value from two operands.

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % remainder

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.

For integer types, / truncates towards zero and % yields the remainder of truncating division, which has the sign of the dividend. Integer arithmetic wraps on overflow: signed integer types wrap in two's complement, and unsigned integer types wrap modulo 2^N, where N is the type's width. For floating-point types, all operators follow IEEE 754 semantics.

Integer division by zero, or integer remainder by zero, produces a runtime error; see the runtime error model.

Assertions

  1. Both operands are numeric types within the same family: signed integer, unsigned integer, or floating-point.
  2. The remainder operator % requires integer operands.
  3. The operands have a common type, and the result has that type.
  4. When the surrounding context requires a type within the operator's applicable family, each operand's expected type is that required type.

Syntax

additive_operator = plus | minus ;

multiplicative_operator = star | solidus | percent ;