Use basic math operators

The four basic ways in combining numbers are adding, subtracting, dividing and multiplying. By using only these four basic operations, you can create any type of complicated mathematical formula.

Addition and subtraction are performed using the operators + and -. Adding 500 to 340, and then subtracting 10 looks as follows:

500 + 340 - 10

In addition to + and -, multiplication is performed by the asterisk *, and division by the forward slash /. Priorities of operators follow the standard mathematical rules: multiplicative operations are always performed first, mapping the expression computes the value 410 and not 800:

40 * 10 + 10

As in standard mathematics, you can change the priorities using parenthesis to group the operations to be done first. The following example results in 800:

40 * (10 + 10)

The last operator for numbers is the exponentiation (or power) operator that raises its left operand to the power of its right, double asterisk ** is used:

2 ** 3     → 8
-3 ** 2    → -9
(-3) ** 2  → 9
2 ** -2    → 0
2.5 ** 2   → 6.25
2.0 ** -2  → 0.25

Notice that the exponentiation operator operates only with powers that are whole numbers: you receive an error if you enter 2**2.5.

This operator behaves differently when raising whole numbers or floating point numbers to negative exponents: when you write 2 ** -2 you intend to generate a whole number, you receive 0 because the actual value of 2-2 is ¼.

Notice also that the power operator binds tighter than the unary minus, so -3**2 is in fact the same as –(3**2). If you want to calculate (-3)², you must state it explicitly using the appropriate parenthesis.

Related Links