1.2 Basic Types
Turing has several basic types we can use to store things in our programs. In this section
we will look at constants of these types. There are two main types we can use for storing
numbers. The int type is used to store integers. The real
type is used to store floating point numbers. The following program uses predefined constants
(maxint and minint) to show the range of allowable integers in a Turing
program:
put "The largest integer is ", maxint
put "The smallest integer is ", minint
|
|
This program produces the following output:
The largest integer is 2147483647
The smallest ineger is -2147483647
|
You can use much larger values for reals. For really large (or small) numbers Turing
can use scientific notation. Turing uses the letter E (or e) to represent powers of 10. For example,
- 1.234E5 means 1.234 x 105
- 765.365e-7 means 765.365 x 10-7
- 0.0000000987 could be written as 9.87E-8 which means 9.87 x 10-8
Reals have 16 digits of accuracy and the exponents (number after E) have a range
of at least -38 to 38. You do not have to put a number before or after a decimal. The number after
E in scientific notation must be an integer. Commas and spaces are not allowed in any numbers
(eg. both 10,853 and 10 853 are invalid).
The int type is known as an ordinal type. For any given integer (with the exception
of maxint and minint) there is exactly one value that immediately precedes it and
exactly one value that immediately follows it. The real type is not an ordinal type.
Why? What number follows 10.8? Is it 10.81, 10.801, 10.8001,... ? For any number you suggest
there is always another number between it and 10.8.
If we wish to store characters we use the string type. Any character you can
type on the keyboard can be put in a string. Strings start and end with double
quotes. Here are some strings:
- "Hello"
- "This is a longer string"
- "This one contains \"quotes\"."
To store only 1 character you can use the char type. Chars start and end with single
quotes. Here are some chars:
- '@'
- '\'' - this is the single quote character
- '3'
Exercise 1.2
- Classify each constant as real, int, string or illegal. If it is illegal, justify your answer.
- -0.0123
- 5E7
- "legal?!?"
- .73
- "valid "string"?"
- 5E6.2
- 33,146
|
- -6E-7
- -0
- $7.21
- "$7.21"
- +45.2
- 8.29E
- 'abcde'
|
- Rewrite each value in two other ways, using E notation.
- 0.000577
- 62119
|
- -740000
- -0.0000002
|
- Rewrite without using E notation.
- 35E-3
- 0.077E4
- -1.2E-4
|
- -5300e-1
- 9.9E0
- -4.08e6
|
| |