Mathematical Operation |
Turing Syntax |
Sample Code
|
Output |
addition |
+ |
put 3 + 5
|
8 |
subtraction |
- |
put 10 - 6
|
4 |
multiplication |
* |
put 4 * 7 |
28 |
division - real numbers |
/ |
put 7 / 3 |
2.333333 |
division - integers - quotient |
div |
put 15 div 6 |
2 |
division - integers - remainder |
mod
|
put 15 mod 6 |
3 |
exponents |
** |
put 6 ** 2 |
36 |
Order of Operations: Use brackets to ensure that Turing does operations in the order you want. Otherwise it will evaluation mathematical statements from left-to-right following the order of operations:
- brackets
- exponents
- multiplication
- division
- addition
- subtraction
Rounding Real Values to Integer Values
Turing provides three ways to round a real value for storage in an integer variable.
- floor - always rounds down to the nearest integer value
- ceil (short for "ceiling") - always rounds up to the nearest integer value
- round - performs a proper mathematical rounding based on the first decimal value
Example Code:
put 3.14 % outputs 3.14 to the screen
put floor(3.14) % rounds down - outputs 3 to the screen
put round(3.14) % rounds properly - outputs 3 to the screen
put ceil(3.14) % rounds up - outputs 4 to the screen
put 2.718 % outputs 2.718 to the screen
put floor(2.718) % rounds down - outputs 2 to the screen
put round(2.718) % rounds properly - outputs 3 to the screen
put ceil(2.718) % rounds up - outputs 3 to the screen
Integer Division
Integer division involves finding two values - the quotient and the remainder. For example, to solve 10/3 (or 10 divided by 3), we ask, "how many times does 3 divide into 10?" The answer is 3 times, so the quotient is 3. Since 3 times 3 is 9, there must be a remainder. 10 minus 9 is 1, so the remainder is 1.
Turing provides two functions for performing integer division.
- div (short for "division") - determine the quotient
- mod (short for "modulo") - determines the remainder
Example Code:
put 10 / 3 % ouputs the real division to the screen; the answer is 3.33333
put 10 div 3 % outputs the quotient of 3
put 10 mod 3 % outputs the remainder of 1
Comments (0)
You don't have permission to comment on this page.