Pascal integers are whole numbers, that is, values without a fractional component. For example, 5, 0, and -2 209
are integers, but 13.5 is not, because it has a decimal point and an associated fractional part. Integers have
a limited range: They can't be greater than 32 767 or less than -32 768. Using integers rather than reals to store
numeric values results in efficient programs because your computer's 8086-family CPU can work with integers quickly;
furthermore, they require only 2 bytes of storage.
Program
IntegerVsRealSpeedTest;
uses
Win32Crt;
var
I : integer;
R : real;
begin
I := 0;
R := 0.0;
Write('Press Enter to start Real loop...');
Readln;
while
R < 30000.0 do R
:= R + 1.0;
Writeln('Done');
Write('Press Enter to start Integer loop...');
Readln;
while
I < 30000 do I
:= I + 1;
Writeln('Done');
end. { IntegerVsRealSpeedTest }
The LongInt (long integer) numeric type has a much larger range than type integer, all the way from -2 147 483
648 to 2 147 483 647 (roughly +/- 2 billion). Long integers are handy in situations that require speed, but need
more range than that afforded by integers. Long integers require 4 bytes of storage each.
Integer Division
To divide two integers, use the DIV operator rather than /, the real division operator.
Overflow
When using integer variables, be on the alert for overflow problems. Overflow occurs when a calculation performed
on two integers produces a value too large to be stored in the space reserved for the result. For example, consider
the following program:
program
Overflow;
var A,B,C:
integer;
begin
A := 1000;
B := 1000;
C := A * B;
Writeln(C);
end. { Overflow }
Because 1 000 000 doesn't fit in the 2-byte space allotted for an integer, C gets the mysterious value 16 960 (16
960 equals the lower 16 bits of 1,000,000 when expressed in binary), and no error is reported.