6.3. Operations on Numbers in LPC

There are some basic operators in LPC:


   +        addition
   -        subtraction
   /        division
   *        multiplication
   %        modulus

These are described in more detail later.

When performing operations on numbers, the following rules apply:

6.3.1. Addition in LPC

Addition is pretty straight forward. The result is the addition of the left-hand-side and the right-hand-side of the + sign.

Examples:


   1 + 2     = 3
   1.0 + 2.0 = 3.0
   1 + 2.0   = 3.0

6.3.2. Subtraction in LPC

Subtraction is also straight foward. The result is the subtraction of the right-hand-side from the left-hand-side of the - sign.

Examples:


   10 - 1     = 9
   10.0 - 1.0 = 9.0
   10.0 - 1   = 9.0

6.3.3. Multiplication in LPC

Multiplication is also straight forward. The result is the product of the right-hand-side with the left-hand-side of the * sign.

Examples:


   5 * 2     = 10
   5.0 * 2.0 = 10.0
   5 * 2.0   = 10.0

6.3.4. Division in LPC

Division is not as straight-forward as the other operators. The result is the left-hand-side divided by the right-hand-side of the / sign.

Examples:


   10 / 2     = 5
   10.0 / 2.0 = 5.0
   10 / 2.0   = 5.0

However, anything divided by 0 will kick up a Divide by zero error.

When you divide one int from another, the remainder is thrown away. This is not true of floats.

Examples:


   10 / 3     = 3
   10.0 / 3.0 = 3.333333
   10 / 3.0   = 3.333333

6.3.5. Modulus in LPC

This operator only works with two ints. You cannot use it with floats. When you use this operator, it gives you the remainder that would occur if you divided the two numbers:

Examples:


   10 % 2 = 0
   10 % 3 = 1
   10 % 4 = 2

6.3.6. Parentheses in LPC

When you're doing a series of operations and want them to be performed in a very specific order, then you should use parentheses.

Examples:


   10 / 5 + 2   = 4
   (10 / 5) + 2 = 4
   10 / (5 + 2) = 1