Arithmetic Expressions

Top  Previous  Next

The common arithmetic operations are done using familiar symbols:


        +   Addition

        -   Subtraction

        *   Multiplication

        /   Division


An arithmetic expression is evaluated from left to right, with multiplication and division done first followed by addition and subtraction. The order of evaluation is important because it affects the result of a calculation.


Sometimes it is necessary to evaluate an expression in a different order. The part of an expression within parentheses is evaluated first.


Here are some examples of arithmetic expressions:


        6 + 4/2      equals 8                   (6+4)/2   equals 5

        6 - 4*2      equals -2                  (6-4)*2   equals 4

        6/2*3        equals 9                   6/(2*3)   equals 1

        6-4+2        equals 4                   6-(4+2)   equals 0

        3*(6-(4-1))  equals 9


Integer division gives a quotient and a remainder. The remainder of the most recent division is gotten from the intrinsic "Rem". For example, 19/5 evaluates to 3, and Rem has the remainder 4.


Note that integer division does not work the same way as division using real numbers. For example, these three expressions are not necessarily equal:


        X/10 * 5

        X*5 / 10

        X * (5/10)


For instance, if X is 15 then the first expression evaluates to 5, the second to 7, and the third to 0.


Integer operations do not give an error if they overflow. Overflowing values wrap around. For example, if you add 32767 + 1, the result is -32768. This is desirable because $7FFF + 1 = $8000, and so forth.