| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

Mathematics in Turing

Page history last edited by Steve Sweeney 7 years, 2 months ago

 

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:

  1. brackets
  2. exponents
  3. multiplication
  4. division
  5. addition
  6. subtraction

 

Rounding Real Values to Integer Values

Turing provides three ways to round a real value for storage in an integer variable.

  1. floor - always rounds down to the nearest integer value
  2. ceil (short for "ceiling") - always rounds up to the nearest integer value
  3. 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.

  1. div (short for "division") - determine the quotient
  2. 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.