Материал готовится,

пожалуйста, возвращайтесь позднее

пожалуйста, возвращайтесь позднее

Standard Data Types and Functions

The data processed by any program may consist of integers, real numbers, or strings

of text, but each type is stored and manipulated differently. Pascal provides the following

standard data types (also referred to as simple or scalar data types):

INTEGER

REAL

CHAR

BOOLEAN

You have already used the INTEGER and REAL types as both numeric constants and

variables. You have also already used arithmetic operators with variables and constants

to build arithmetic expressions, and you were introduced to some functions

such as ROUND and TRUNC. This chapter presents the whole picture of numeric

data types and related functions and expressions. It also introduces the type CHAR,

which is used to represent single characters, and the type BOOLEAN, which is used

to represent logical values. The discussion of the single character type contains an

overview of how strings were represented in standard Pascal and also how they are

represented in the modern implementations such as Turbo Pascal and UCSD Pascal

(using the type STRING).

Numeric Data Types

The range of numbers that may be represented as integers (or as reals) depends on

the implementation. For the type INTEGER it is determined by the following limits:

MAXINT the maximum positive integer

–(MAXINT+1) the maximum negative integer

Again, the value of MAXINT depends on the implementation.

Real numbers are generally stored in a larger number of bytes than are integers, but

they are of limited precision. Fractions such as 0.333333 and 0.666666 can never be as

precise as the exact values 1/3 and 2/3, regardless of how many digits are used to represent

the number. For this reason, it is not recommended to test two real numbers

for equality. Instead, it would be better to test to see if the difference between the two

numbers is less than some specific small amount.

In Turbo Pascal, there are additional numeric types, which are introduced in the following

section.

Numeric Types in Turbo Pascal

There are additional integer types (including the type INTEGER) in Turbo Pascal.

They are shown in Table 2-1 along with their storage sizes and the maximum range of

values that can be represented in each.

In one byte, you can store either a SHORTINT or a BYTE. The BYTE is actually an

unsigned SHORTINT, which means that it can hold only positive numbers. As you can

see in the table, the maximum range of values for a type is doubled when the sign is

not used. The same applies to the types INTEGER and WORD, as the WORD is a positive

integer of doubled maximum range.

Table 2-1 Turbo Pascal integer types

Data Type Size (in bytes) Range

SHORTINT 1 from –128 to +127

BYTE 1 from 0 to 255

INTEGER 2 from –32768 to +32767

WORD 2 from 0 to 65535

The LONGINT is the largest integer that can be represented in Turbo Pascal. You can

test its value by displaying the value of the predefined constant MAXLONGINT as

follows:

WRITELN(MAXLONGINT);

Notice that the negative range of any signed type exceeds the positive range by one

(e.g., +127 and –128). This is because zero is counted with the positive numbers.

CAUTION The commas used here to express large numbers are used only for

readability. You will neither see them in the output of a program, nor

are they accepted as a part of literal constants. So, the number 2,147,483,647 must

be used as 2147483647.

In Turbo Pascal, there are also additional real types (including the type REAL) as

shown in Table 2-2.

The data processed by any program may consist of integers, real numbers, or strings

of text, but each type is stored and manipulated differently. Pascal provides the following

standard data types (also referred to as simple or scalar data types):

INTEGER

REAL

CHAR

BOOLEAN

You have already used the INTEGER and REAL types as both numeric constants and

variables. You have also already used arithmetic operators with variables and constants

to build arithmetic expressions, and you were introduced to some functions

such as ROUND and TRUNC. This chapter presents the whole picture of numeric

data types and related functions and expressions. It also introduces the type CHAR,

which is used to represent single characters, and the type BOOLEAN, which is used

to represent logical values. The discussion of the single character type contains an

overview of how strings were represented in standard Pascal and also how they are

represented in the modern implementations such as Turbo Pascal and UCSD Pascal

(using the type STRING).

Numeric Data Types

The range of numbers that may be represented as integers (or as reals) depends on

the implementation. For the type INTEGER it is determined by the following limits:

MAXINT the maximum positive integer

–(MAXINT+1) the maximum negative integer

Again, the value of MAXINT depends on the implementation.

Real numbers are generally stored in a larger number of bytes than are integers, but

they are of limited precision. Fractions such as 0.333333 and 0.666666 can never be as

precise as the exact values 1/3 and 2/3, regardless of how many digits are used to represent

the number. For this reason, it is not recommended to test two real numbers

for equality. Instead, it would be better to test to see if the difference between the two

numbers is less than some specific small amount.

In Turbo Pascal, there are additional numeric types, which are introduced in the following

section.

Numeric Types in Turbo Pascal

There are additional integer types (including the type INTEGER) in Turbo Pascal.

They are shown in Table 2-1 along with their storage sizes and the maximum range of

values that can be represented in each.

In one byte, you can store either a SHORTINT or a BYTE. The BYTE is actually an

unsigned SHORTINT, which means that it can hold only positive numbers. As you can

see in the table, the maximum range of values for a type is doubled when the sign is

not used. The same applies to the types INTEGER and WORD, as the WORD is a positive

integer of doubled maximum range.

Table 2-1 Turbo Pascal integer types

Data Type Size (in bytes) Range

SHORTINT 1 from –128 to +127

BYTE 1 from 0 to 255

INTEGER 2 from –32768 to +32767

WORD 2 from 0 to 65535

The LONGINT is the largest integer that can be represented in Turbo Pascal. You can

test its value by displaying the value of the predefined constant MAXLONGINT as

follows:

WRITELN(MAXLONGINT);

Notice that the negative range of any signed type exceeds the positive range by one

(e.g., +127 and –128). This is because zero is counted with the positive numbers.

CAUTION The commas used here to express large numbers are used only for

readability. You will neither see them in the output of a program, nor

are they accepted as a part of literal constants. So, the number 2,147,483,647 must

be used as 2147483647.

In Turbo Pascal, there are also additional real types (including the type REAL) as

shown in Table 2-2.

Загрузка...

Выбрать следующее задание

Ты добавил

Выбрать следующее задание

Ты добавил