Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 2
Language Elements

2-1 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 scaler 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 tasted the flavor of some functions such as ROUND and TRUNC. This chapter introduces 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 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).

2-2 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
LONGINT 4 from -2,147,483,648 to +2,147,483,647

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. For real numbers, a new column is added to the table to describe the accuracy of a number as the maximum number of precise digits.

Table 2-2 Turbo Pascal Real Types

Data Type Size (in bytes) Precision (up to) Range

SINGLE 4 7 digits from 0.71E-45 to 3.4E+38
REAL 6 11 digits from 2.94E-39 to 1.7E+38
DOUBLE 8 15 digits from 4.94E-324 to 1.79E+308
EXTENDED 10 19 digits from 3.3E-4932 to 1.18E+4932
COMP 8 integers only ±9.2E+18

If you examine the range of the type SINGLE, you will find that it is pretty close to that of the type REAL, especially in the area of the very large numbers. The main difference between the two lies in the economical storage of the SINGLE type (4 bytes compared to 6), which comes at the expense of precision (7 digits compared to 11). Real number types other than REAL are not available unless a math coprocessor is used. The type COMP actually belongs to the set of integers, as it does not accept fractions, but it is usually mentioned among reals as it requires the use of a math coprocessor.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.