Computers are designed to carry out calculations. Mathematical operators allow arithmetic to be performed on values:
Mathematical operation
Operator
Example
Addition
+
x = x + 5
Subtraction
-
x = x - 5
Multiplication
*
x = x * 5
Division
/
x = x / 5
Integer division
DIV
x = x DIV 5
Remainder
MOD
x = x MOD 5
Exponentiation
^
x = x^2
Mathematical operation
Addition
Operator
+
Example
x = x + 5
Mathematical operation
Subtraction
Operator
-
Example
x = x - 5
Mathematical operation
Multiplication
Operator
*
Example
x = x * 5
Mathematical operation
Division
Operator
/
Example
x = x / 5
Mathematical operation
Integer division
Operator
DIV
Example
x = x DIV 5
Mathematical operation
Remainder
Operator
MOD
Example
x = x MOD 5
Mathematical operation
Exponentiation
Operator
^
Example
x = x^2
Calculations carried out by computers follow the same principles used when working out sums manually.
Addition
Addition uses the ‘+’ symbol:
6 + 6
x + y
x = x + 2
x = x + y
Subtraction
Subtraction uses the ‘-’ symbol:
8 - 6
x - y
x = x - 2
x = x - y
Multiplication
Multiplication uses the ‘*’ symbol:
5 * 5
x * y
x = x * 5
x = x * y
Division
Division uses the ‘/’ symbol:
6 / 2
x / y
x = x / 2
x = x / y
Modulus
Modulus gives the remainder from a division calculation:
7 MOD 3 = 1 (7 / 3 = 2 remainder 1)
Many programming languages, such as Python, use the ‘%’ character to represent modulus:
6 % 2
x % y
x = x % 2
x = x % y
Integer division
Integer division (also known as floor division) discards the remainder:
7 DIV 3 = 2 (7 / 3 = 2 remainder 1 – the remainder is discarded)
Some programming languages, such as Python, use ‘//’ to represent integer division:
6 // 3
x // y
x = x // 2
x = x // y
Exponentiation
Exponentiation uses powers represented by the symbol ‘^’:
2 ^ 2
= 2 x 2
= 4
2 ^ 3
= 2 x 2 x 2
= 8
2 ^ 4
= 2 x 2 x 2 x 2
= 16
Some programming languages, such as Python, use ‘**’ to represent exponentiation:
2 ** 2
x ** 2
x ** y
Use of brackets
Calculations within brackets are performed first. If there are no brackets, multiplication is performed first, then division, addition and finally, subtraction:
For example:
(4 + 2) * 5
= 6 * 5
= 30
Whereas:
4 + 2 * 5
= 4 + 10
= 14
4 - 2 / 2
= 4 - 1
= 3
4 * 2 / 4
= 8 / 4
= 2
4 + 2 - 3
= 6 - 3
= 3
Where multiple brackets exist, the inside brackets are dealt with first: