Concurrent programming in Erlang

2.7. Arithmetic Expressions

Arithmetic expressions are formed from the following operators:

Operator Description Type Operands Prio
+ X + X unary mixed 1
- X - X unary mixed 1
X * Y X * Y binary mixed 2
X / Y X / Y (floating point division) binary mixed 2
X div Y integer division of X and Y binary integer 2
X rem Y integer remainder of X divided by Y binary integer 2
X band Y bitwise and of X and Y binary integer 2
X + Y X + Y binary mixed 3
X - Y X - Y binary mixed 3
X bor Y bitwise or of X and Y binary integer 3
X bxor Y arithmetic bitwise xor X and Y binary integer 3
X bsl N arithmetic bitshift left of X by N bits binary integer 3
X bsr N bitshift right of X by N bits binary integer 3

Unary operators have one argument, binary operators have two arguments.

Mixed means that the argument can be either an integer or float. Unary oper- ators return a value of the same type as their argument.

The binary mixed operators (i.e. *, -, +) return an object of type integer if both their arguments are integers, or float if at least one of their arguments is a float. The floating point division operator / returns a float irrespective of its arguments.

Binary integer operators (i.e. band, div, rem, bor, bxor, bsl, bsr) must have integer arguments and return integers.

The order of evaluation depends upon the priority of the operator: all priority 1 operators are evaluated, then priority 2, etc. Any bracketed expressions are evaluated first.

Operators with the same priority are evaluated left to right. For example:

A - B - C - D

is evaluated as if it had been written:

(((A - B) - C) - D)