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


1-3 Crunching Numbers

The easiest task for any program is to crunch numbers. The statement WRITELN (or WRITE) can be used both to display numbers and evaluate numerical expressions. You can build up arithmetic expressions using the following arithmetic operators:

+ for addition
- for subtraction
* for multiplication
/ for division

Take a look at these examples:

    WRITELN(123);
    WRITELN(1.23 * 4);

The first example displays the number that came between the parentheses (123). The second example performs multiplication of two numbers and displays the result. Notice that for numeric values, unlike text strings, you don’t use quotes.

You may use WRITELN to display text and numbers in the same statement by using the comma as a separator like this:

    WRITELN('The result is=', 125 * 1.75);

The following program is used to evaluate two numeric expressions (multiplication and division) and display the results preceded by the proper text.

{ --------------------------- figure 1-6 ---------------------------}
PROGRAM CrunchNumbers(OUTPUT);
BEGIN
  WRITELN('I can easily crunch numbers.');
  WRITELN('Here is multiplication of 50x4:',50*4);
  WRITELN('..and here is division of 2400/8:',2400/8)
END.

The output of this program is:

I can easily crunch numbers.
Here is multiplication of 50x4:200
..and here is division of 2400/8: 3.0000000000E+02

The multiplication is done as expected. The two operands (50 and 4) were integers (whole numbers) and the result (200) was an integer too. The division result, however, came out in a format that needs some explanation.

Integers and Real Numbers

The division performed with the operator / is called real division and always produces as its result a real number. Real numbers may be written in fixed-point notation (such as 300.0) or in scientific (exponential) notation (such as 3.0E+02), but in Pascal, real number output will always be represented in scientific notation by default. A number written in scientific notation is made up of two parts divided by the letter E (or e). The left part is called the mantissa and indicates the significant digits, while the right part is called the exponent. The exponent is a power of ten that determines the position of the decimal point. So, in this example the number:

3.0000000000E+02

is the same as the number:

3 x 102

The same number, when expressed in fixed-point format, becomes:

300.0

If the exponent is preceded by a minus sign as in:

3.124E-02

then the decimal point is shifted two positions to the left. This number, then, is the same as:

0.03124

If the number is negative, the minus sign should precede the mantissa:

-0.0124E-02

If the number is positive, you may omit the sign for either the mantissa or the exponent:

1.23E02

The division operator (/) is called the real division operator, because the result always appears as a real number regardless of the type of the operands.

For integer division use the operator DIV as in the example:

    WRITELN(2400 DIV 8);

This will produce the output 300.

With integer division, any fraction in the result will be truncated, as in this example:

    WRITELN(9 DIV 4);              produces the output 2.

Another important operator, MOD, is used to get the remainder of integer division (modulo), as in these examples:

    WRITELN(9 MOD 4);              produces the output 1.
    WRITELN(3 MOD 4);              produces the output 3.

The operators DIV and MOD take only integer operands and produce integer output. For the other operators (+, -, and *), if either one of the operands is real, the result will be real.

Drill 1-2

Evaluate the following expressions and write the result either as an integer (if integer), or as a fixed-point real number (if real):

A.  144 / 12
B.  144 DIV 12
C.  17 MOD 5
D.  3 MOD 5
E.  3e+02 + 3
F.  345E-01 - 1


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.