2.1 Arithmetic

You can do arithmetic in Turing very much the way you do in regular math. The order of operations is the same. The symbols for addition (+) and subtraction (-) are exactly the same. The symbol for multiplication (*) is different since an x could be confused with the letter. The symbol for division (/) is used since there is no easy way to type ÷ on the keyboard. The symbol for exponents (**) is used since there is no easy way to do superscripts on the keyboard.

To compute 2 - 5 * (2 + 3) / 2 in Turing you would follow these steps:

2 - 5 * (2 + 3) / 2
= 2 - 5 * (5) / 2
= 2 - 25 / 2
= 2 - 12.5
= - 10.5
Notice that / produces a real answer (and it would even if the number divides evenly). Notice that subtracting an int and a real produces a real. In fact, any calculations involving ints and reals will produce a real result. Here is an example program that will produce an error:

var a : int

a := 10 / 5 % even though the answer is exactly 2 it will still be real
            % and you can't store a real in an int
put a 

It is possible to do integer division. To do that you use the keyword div instead of the slash character (/). If you want to know the remainder after doing an integer division you can use the keyword mod. The div and mod operators have the same precedence as division and multiplication. That is, div, mod, * and / all get done before addition and subtraction. Here is an example to demonstrate these ideas:

put 35 div 8
put 35 mod 8
put -8 div -3
put -8 div 3
put 49 div 10 * 3 - 4 mod 2 + 1.1
put 2 + 3 - 5 * 8 / 7 + 3
put (2 + 3) - (5 * 8 / 7 + 3) 

It produces this output:

4
3
2
-2
13.1
2.285714
-3.714286

You can use Turing to calculate powers. The number before the ** operator is the base and the number after is the exponent. Here are some examples:


Exercise 2.1

  1. Evaluate each valid expression and state the type of the result. If the expression is invalid, give the reason.
    1. 2 + 7 / 2
    2. 55 / 5
    3. ( 8 - 3 * 2) * 4 - 1
    4. 16 - 24 / 6 / 2
    5. 5(6 + 3)
    1. 20 / (8 - 2)
    2. 2E+02 + 02
    3. 20 * .4
    4. 4 - [6 - (2 - 7)]
    5. 4e-3 + 2e-1
  2. Evaluate each valid expression. If the expression is invalid, give the reason.
    1. 39 div 3
    2. 39 mod 3
    3. 24 div -5
    4. 27 mod 4
    5. 15 div 24
    6. 24.5 div 3
    7. -15 div 7
    1. -4 div 9
    2. -56 div -11
    3. -86 div 20
    4. 46 div 9 div 2
    5. 23 mod 13 mod 3
    6. 37 div 8 mod 2
    7. 5 div (8 div 10)
  3. Evaluate.
    1. 5 * (20 div 5)
    2. 48 div 6 * 6
    3. (65 div 10) * 10
    4. 6 * (28 div 6)
    5. 3 * (20 div 3) + 20 mod 3
    6. 8 * (60 div 8) + 60 mod 8
    7. 17 * (99 div 17) + 99 mod 177
    8. 7 *(-24 div 7) + (-24) mod 7
    9. -20 div 9 * 9 - 20 mod 9
    10. (-13 div 3) * 3 + (-13 mod 3)
  4. Assuming that the following assignments have been made:
    var m : int := 30   var n : int := 40
    var x : real := 1.5   var y : real := 4.0 
    evaluate each expression, writing real valued answers correct to four decimal places
    1. 5 * (x - (-2) div 7) + n div m mod 4
    2. m * (n + 28 div (n div 12)) - x
    3. (x + 5) * ((-y / 8e0 + 7e1) / 2e1 + 1)
    4. (m - 10) / (n + 10) * (x + y) / (x - y)
  5. Write as a Turing expression, using the fewest brackets possible. (Assume that x and y are real.)
    1. x3 + y3
  6. Evaluate each valid expression. If the expression is invalid, give the reason.
    1. 2 ** 8
    2. 10.0 ** - 5
    3. 8 ** (1 / 3)
    1. 16 ** (- 1 / 2)
    2. -25 ** 0.5
    3. (-36) ** 0.5