1.4

Standard Primitive Types

Standard primitive types are those types that are available on most computers as built-in features. They include the whole numbers, the logical truth values, and a set of printable characters. On larger computers fractional numbers are also incorporated, together with an adequate set of primitive operators. We denote these types by the identifiers

integer,  Boolean,  char,  real

The type integer comprises a subset of the whole numbers whose size may vary between individual computer systems. It is assumed, however, that all operations on data of this type are exact and correspond to the ordinary laws of arithmetic and that the computation will be interrupted in the case of a result lying outside the representable subset. The standard operators are the four basic arithmetic operations of addition (++), subtraction (−-), multiplication (∗*), and division (div). The latter is understood to yield an integer result, ignoring a possible remainder, such that, for positive mm and nn

m−n<(mdivn)∗n≤m(1.3)m-n < (m \mathbin{\mathbf{div}} n)*n \le m \tag{1.3}

The modulus operator is defined in terms of division by the equation

(mdivn)∗n+(mmodn)=m(1.4)(m \mathbin{\mathbf{div}} n)*n+(m \mathbin{\mathbf{mod}} n) = m \tag{1.4}

Thus, m div n is the integer quotient of mm and nn, and m.mod n is the associated remainder.

The type real denotes a subset of the real numbers. Whereas arithmetic involving integers only is assumed to yield exact results, arithmetic on values of type real is permitted to be inaccurate within the limits of round-off errors caused by computation on a finite number of digits. This is the principal reason for the explicit distinction between the types integer and real made in most programming languages.

We denote division of real numbers yielding a real valued quotient by a slash ( / ) in contrast to the div of integer division.

The two values of the standard type Boolean are denoted by the identifiers9 true and false. The Boolean operators are the logical conjunction, union, and negation whose values are defined in Table 1.1. The logical conjunction

Table 1.1 Boolean Operators.
Table 1.1 Boolean Operators.

is denoted by the symbol ∧\land (or and), the logical union by ∨\lor (or or), and negation by ¬\neg (or not). Note that comparisons are operators yielding a result of type Boolean. Thus, the result of a comparison may be assigned to a variable, or it may be used as an operand of a logical operator in a Boolean expression. For instance, given Boolean variables pp and qq and integer variables x=5x = 5, y=8y = 8, z=10z = 10, the two assignments

p := x = y
q := (x<y) ∧ (y≤z)

yield p=falsep = \mathit{false} and q=trueq = \mathit{true}.

The standard type char comprises a set of printable characters. Unfortunately, there is no generally accepted standard character set used on all computer systems. Therefore, the use of the predicate “standard” may in this case be almost misleading; it is to be understood in the sense of “standard on the computer system on which a certain program is to be executed.”

The character set defined by the International Standards Organization (ISO), and particularly its American version ASCII (American Standard Code for Information Interchange) is probably the most widely accepted set. The ASCII set is therefore tabulated in Appendix A. It consists of 95 printable (graphic) characters and 33 control characters, the latter mainly being used in data transmission and for the control of printing equipment. A subset with 64 printable characters (capital letters only) is widely used and called the restricted ASCII set.

In order to be able to design algorithms involving characters (i.e., values of type char) that are computer system independent, we should like to be able to assume certain properties of character sets as binding, namely,

  1. The type char contains the 26 Latin letters, the 10 Arabic digits, and a number of other graphic characters, such as punctuation marks.

  2. The subsets of letters and digits are ordered and coherent, i.e.,

    (‘A’≤x)∧(x≤‘Z’)≡x is a letter(‘0’≤x)∧(x≤‘9’)≡x is a digit(1.5)\begin{aligned} (\text{‘A’} \le x) \land (x \le \text{‘Z’}) &\equiv x \text{ is a letter} \\ (\text{‘0’} \le x) \land (x \le \text{‘9’}) &\equiv x \text{ is a digit} \end{aligned} \tag{1.5}

10 3. The type char contains a non-printing, blank character which may be used as a separator. (Blanks are denoted by ␣ in Fig. 1.1).

Fig. 1.1 Representations of a text.
Fig. 1.1 Representations of a text.

The availability of two standard type transfer functions between the types char and integer is particularly important in the quest to write programs in a machine independent form. We will call them ord(c), denoting the ordinal number of character cc in the set char, and chr(i), denoting the iith character in the set char. Thus, chr is the inverse function of ord, and vice versa, that is,

ord(chr(i))=i(if chr(i) is defined)chr(ord(c))=c(1.6)\begin{aligned} \mathit{ord}(\mathit{chr}(i)) &= i \qquad (\text{if } \mathit{chr}(i) \text{ is defined}) \\ \mathit{chr}(\mathit{ord}(c)) &= c \end{aligned} \tag{1.6}

Particularly noteworthy are the functions

f(c)=ord(c)−ord(‘0’)=the position of c among the digitsg(i)=chr(i+ord(‘0’))=the i’th digit(1.7)\begin{aligned} f(c) &= \mathit{ord}(c) - \mathit{ord}(\text{‘0’}) &&= \text{the position of } c \text{ among the digits} \\ g(i) &= \mathit{chr}(i+\mathit{ord}(\text{‘0’})) &&= \text{the } i\text{’th digit} \end{aligned} \tag{1.7}

For example, f(‘3’)=3f(\text{‘3’}) = 3, g(5)=‘5’g(5) = \text{‘5’}. ff is the inverse function of gg, and vice versa, i.e.,

f(g(i))=i(0≤i≤9)g(f(c))=c(‘0’≤c≤‘9’)(1.8)\begin{aligned} f(g(i)) &= i \qquad && (0 \le i \le 9) \\ g(f(c)) &= c \qquad && (\text{‘0’} \le c \le \text{‘9’}) \end{aligned} \tag{1.8}

These transfer functions are used in the conversion of internal representations of numbers into sequences of digits, and vice versa. In fact, they represent these conversions on the most elementary level, namely that of a single digit.

↑ Up