Integer

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) suomi (fi) français (fr) italiano (it) русский (ru)

The data type integer is a built-in data type of the programming language Pascal. It can store a subset of ℤ, the set of whole numbers.

integer literal

basics

An integer is specified as a non-empty series of consecutive Western-Arabic digits.

1234

The value of 1234 is [math]\displaystyle{ 1 \times 10^3 + 2 \times 10^2 + 3 \times 10^1 + 4 \times 10^0 }[/math]. The integer may be preceded by a sign, + or , even if mathematically speaking the value is signless (this concerns the value zero).

+0   { ✔ syntactically correct }

If no sign is specified, a positive sign is presumed.

In Free Pascal, you can use the underscore to group digits if {$modeSwitch underscoreIsSeparator+} (as of 2022 only available in trunk).

1_000_000_000

varying base

To change the base in Extended Pascal you prefix the integer literal with a base specification:

8#1234

This represents the value [math]\displaystyle{ 1 \times 8^3 + 2 \times 8^2 + 3 \times 8^1 + 4 \times 8^0 }[/math]. The base can be any value between 2 and 36 (inclusive) and can only be specified to a decimal base. A base specification possibly extends or restricts the set of allowed digits to those in the set of 0 to base  1 from below table:

integer digits in Extended Pascal
digit (case-insensitive) 0 1 2 3 4 5 6 7 8 9 A B
value (decimal) [math]\displaystyle{ 0 }[/math] [math]\displaystyle{ 1 }[/math] [math]\displaystyle{ 2 }[/math] [math]\displaystyle{ 3 }[/math] [math]\displaystyle{ 4 }[/math] [math]\displaystyle{ 5 }[/math] [math]\displaystyle{ 6 }[/math] [math]\displaystyle{ 7 }[/math] [math]\displaystyle{ 8 }[/math] [math]\displaystyle{ 9 }[/math] [math]\displaystyle{ 10 }[/math] [math]\displaystyle{ 11 }[/math]
digit (case-insensitive) C D E F G H I J K L M N
value (decimal) [math]\displaystyle{ 12 }[/math] [math]\displaystyle{ 13 }[/math] [math]\displaystyle{ 14 }[/math] [math]\displaystyle{ 15 }[/math] [math]\displaystyle{ 16 }[/math] [math]\displaystyle{ 17 }[/math] [math]\displaystyle{ 18 }[/math] [math]\displaystyle{ 19 }[/math] [math]\displaystyle{ 20 }[/math] [math]\displaystyle{ 21 }[/math] [math]\displaystyle{ 22 }[/math] [math]\displaystyle{ 23 }[/math]
digit (case-insensitive) O P Q R S T U V W X Y Z
value (decimal) [math]\displaystyle{ 24 }[/math] [math]\displaystyle{ 25 }[/math] [math]\displaystyle{ 26 }[/math] [math]\displaystyle{ 27 }[/math] [math]\displaystyle{ 28 }[/math] [math]\displaystyle{ 29 }[/math] [math]\displaystyle{ 30 }[/math] [math]\displaystyle{ 31 }[/math] [math]\displaystyle{ 32 }[/math] [math]\displaystyle{ 33 }[/math] [math]\displaystyle{ 34 }[/math] [math]\displaystyle{ 35 }[/math]

As of version 3.2.0, the FPC intends to ({$mode extendedPascal}), but does not yet support a generic base-specification format. Instead only the following bases are recognized:

integer base specifications in FPC 3.2.0
base indicator sample (decimal value)
binary ([math]\displaystyle{ 2 }[/math]) % %1010 ([math]\displaystyle{ 10 }[/math])
octal ([math]\displaystyle{ 8 }[/math]) & &644 ([math]\displaystyle{ 420 }[/math])
decimal ([math]\displaystyle{ 10 }[/math]) none 1337 ([math]\displaystyle{ 1337 }[/math])
hexadecimal ([math]\displaystyle{ 16 }[/math]) $ $2A ([math]\displaystyle{ 42 }[/math])

characteristics

It is guaranteed that all arithmetic operations in the range maxInt..+maxInt work accurately. An integer variable may possibly store values beyond this range, but once you leave this range it is not guaranteed anymore that arithmetic operations work correctly. Textbook example:

program lordOverflowStrikesAgain(output);
	{$overflowChecks on}
	var
		x: integer;
	begin
		x := -maxInt;
		x := pred(x); { If this doesn’t cause an error, `-maxInt - 1` storable. }
		writeLn(abs(x));
	end.

Depending on the processor used, this may print:

-9223372036854775808

A quite unexpected result since abs should in principle return a non-negative value, yet expectable since pred(maxInt) is evidently not in the maxInt..+maxInt range.

application

Integer is the data of choice if arithmetic results have to be precise. The data type real may provide “reasonable approximations”, but operations on integer have to be exact (only guaranteed as long as it is in the maxInt..+maxInt range). Generally speaking integer operations are also faster than if done in the domain of real.

The operators div and mod only work on integer values (the math unit provides the fMod function and overloads mod).

Free Pascal deviations

The FPC does not have a single data type integer but a host of integer data types. An integer literal such as 123 possesses the data type of closest fitting range from the following table.

integer data types in FPC version 3.2.0
name (aliases) smallest storable value largest storable value sizeOf
shortInt (int8) -128 ([math]\displaystyle{ -2^7 }[/math]) 127 ([math]\displaystyle{ 2^7-1 }[/math]) 1
byte (uInt8) 0 ([math]\displaystyle{ 0 }[/math]) 255 ([math]\displaystyle{ 2^8-1 }[/math]) 1
smallInt (int16) -32768 ([math]\displaystyle{ -2^{15} }[/math]) 32767 ([math]\displaystyle{ 2^{15}-1 }[/math]) 2
word (uInt16) 0 ([math]\displaystyle{ 0 }[/math]) 65535 ([math]\displaystyle{ 2^{16}-1 }[/math]) 2
longInt (int32) -2147483648 ([math]\displaystyle{ -2^{31} }[/math]) 2147483647 ([math]\displaystyle{ 2^{31}-1 }[/math]) 4
longWord (cardinal, dWord) 0 ([math]\displaystyle{ 0 }[/math]) 4294967295 ([math]\displaystyle{ 2^{32}-1 }[/math]) 4
int64 -9223372036854775808 ([math]\displaystyle{ -2^{63} }[/math]) 9223372036854775807 ([math]\displaystyle{ 2^{63}-1 }[/math]) 8
qWord (uInt64) 0 ([math]\displaystyle{ 0 }[/math]) 18446744073709551615 ([math]\displaystyle{ 2^{64}-1 }[/math]) 8

The signed ranges are preferred (i. e. as in the top/down order in the table), thus 123 possesses the data type shortInt even though it could be a byte, too.

As of version 3.2.0, the data type integer is simply an alias depending on the currently selected compiler compatibility mode. It does not depend on the CPU type, therefore it is quite possible that the CPU could in fact deal with integers having an even larger magnitude than integer provides.

the data type integer in FPC version 3.2.0
mode integer is an alias for value of maxInt
{$mode FPC}, {$mode macPas} and {$mode TP} smallInt 32767
all other available modes longInt 2147483647
Warning-icon.png

Warning: Undocumented feature: General programming advice, do not use what has not been documented. Unlikely as it may be, the following feature may be removed at any time.

Depending on the platform’s arithmetic logic unit’s word size (ALU) following aliases are available.

type
	{$ifDef CPU16} ALUSInt = smallInt; ALUUInt = word;  {$endIf}
	{$ifDef CPU32} ALUSInt = longInt;  ALUUInt = dWord; {$endIf}
	{$ifDef CPU64} ALUSInt = int64;    ALUUInt = qWord; {$endIf}

Although it is frequently the case that the ALU’s word size also coincides with the size of a pointer, it is not guaranteed (e. g. the x32-ABI uses 64‑bit ALU, but only 32‑bit pointers). Therefore if an integer value is meant to be typecasted to a pointer, it recommended to use ptrUInt. Note, the data type nativeInt is in fact an alias for ptrInt and not related to ALUSInt.

see also


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring