Difference between revisions of "Integer"

From Free Pascal wiki
Jump to navigationJump to search
 
(29 intermediate revisions by 17 users not shown)
Line 1: Line 1:
<small>
+
{{Integer}}
[[integer/it|'''italiano (de)''']]
 
[[integer | English (en)]]
 
</small>
 
  
 +
The [[Data type|data type]] <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> is a [[Standard type|built-in]] data type of the programming language [[Pascal]].
 +
It can store a subset of&nbsp;ℤ, the set of whole numbers.
  
'''Integer''' is a [[standard type]] of the [[Pascal]] Programming language. It is used to define a whole number, as opposed to a [[real]] data type, used to define a floating point number which may contain a decimal point and possibly an exponent.
+
== <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> literal ==
 +
=== basics ===
 +
An <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> is specified as a non-empty series of consecutive Western-Arabic digits.
 +
<syntaxhighlight lang="pascal">1234</syntaxhighlight>
 +
The value of <syntaxhighlight lang="pascal" inline>1234</syntaxhighlight> is <math>1 \times 10^3 + 2 \times 10^2 + 3 \times 10^1 + 4 \times 10^0</math>.
 +
The <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> may be preceded by a sign, [[Plus|<syntaxhighlight lang="pascal" inline>+</syntaxhighlight>]] or [[Minus|<syntaxhighlight lang="pascal" inline>−</syntaxhighlight>]], even if ''mathematically speaking'' the value is signless (this concerns the value zero).
 +
<syntaxhighlight lang="pascal">+0  { ✔ syntactically correct }</syntaxhighlight>
 +
If no sign is specified, a positive sign is presumed.
  
The size of an integer is dependent upon the bit size of the target machine to which the compiler is to generate code ([[32 bit]] or [[64 bit]]), the type of [[compiler]] (16-bit, 32-bit or 64-bit), and upon compiler switches in some cases.  Typical sizes of integer generally are 16 bit, (2 byte) 32 bit (4 byte) or 64 bit (8 byte).
+
In Free Pascal, you can use the [[Underscore|underscore]] to group digits if <syntaxhighlight lang="delphi" inline>{$modeSwitch underscoreIsSeparator+}</syntaxhighlight> (as of 2022 only [[FPC New Features Trunk#Support for Digit Separator|available in trunk]]).
 +
<syntaxhighlight lang="delphi">1_000_000_000</syntaxhighlight>
  
FPC currently uses 32 bits (2 bytes) for integers, whether the machine is a 32-bit or 64-bit machine. This will cause code expecting an integer and a pointer to be the same size to fail as a 64-bit machine uses 64-bit pointers. To allow you to write portable code, the FPC system unit introduces the types [[ptrint|PtrInt]] and [[ptruint|PtrUInt]] which are signed and unsigned integer data types with the same size as a pointer.
+
=== varying base ===
 +
To change the base in [[Extended Pascal]] you prefix the <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> literal with a base specification:
 +
<syntaxhighlight lang="pascal">8#1234</syntaxhighlight>
 +
This represents the value <math>1 \times 8^3 + 2 \times 8^2 + 3 \times 8^1 + 4 \times 8^0</math>.
 +
The base can be any value between <syntaxhighlight lang="pascal" inline>2</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>36</syntaxhighlight> (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 <syntaxhighlight lang="pascal" inline>0</syntaxhighlight> to <syntaxhighlight lang="pascal" inline>base − 1</syntaxhighlight> from below table:
 +
{| class="wikitable" style="text-align: right; margin: auto;"
 +
|+ <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> digits in Extended Pascal
 +
! scope="row" | digit (case-insensitive)
 +
| <syntaxhighlight lang="pascal" inline>0</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>1</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>2</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>3</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>4</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>5</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>6</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>7</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>8</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>9</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>A</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>B</syntaxhighlight>
 +
|-
 +
! scope="row" | value (decimal)
 +
| <math>0</math>
 +
| <math>1</math>
 +
| <math>2</math>
 +
| <math>3</math>
 +
| <math>4</math>
 +
| <math>5</math>
 +
| <math>6</math>
 +
| <math>7</math>
 +
| <math>8</math>
 +
| <math>9</math>
 +
| <math>10</math>
 +
| <math>11</math>
 +
|-
 +
! scope="row" | digit (case-insensitive)
 +
| <syntaxhighlight lang="pascal" inline>C</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>D</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>E</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>F</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>G</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>H</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>I</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>J</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>K</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>L</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>M</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>N</syntaxhighlight>
 +
|-
 +
! scope="row" | value (decimal)
 +
| <math>12</math>
 +
| <math>13</math>
 +
| <math>14</math>
 +
| <math>15</math>
 +
| <math>16</math>
 +
| <math>17</math>
 +
| <math>18</math>
 +
| <math>19</math>
 +
| <math>20</math>
 +
| <math>21</math>
 +
| <math>22</math>
 +
| <math>23</math>
 +
|-
 +
! scope="row" | digit (case-insensitive)
 +
| <syntaxhighlight lang="pascal" inline>O</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>P</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>Q</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>R</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>S</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>T</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>U</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>V</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>W</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>X</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>Y</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>Z</syntaxhighlight>
 +
|-
 +
! scope="row" | value (decimal)
 +
| <math>24</math>
 +
| <math>25</math>
 +
| <math>26</math>
 +
| <math>27</math>
 +
| <math>28</math>
 +
| <math>29</math>
 +
| <math>30</math>
 +
| <math>31</math>
 +
| <math>32</math>
 +
| <math>33</math>
 +
| <math>34</math>
 +
| <math>35</math>
 +
|}
 +
As of version 3.2.0, the [[FPC]] intends to ([[Mode extendedpascal|<syntaxhighlight lang="delphi" inline>{$mode extendedPascal}</syntaxhighlight>]]), but does not yet support a generic base-specification format.
 +
Instead only the following bases are recognized:
 +
{| class="wikitable" style="margin: auto;"
 +
|+ <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> base specifications in FPC 3.2.0
 +
! scope="column" | base                            !! scope="column" | indicator                                                !! scope="column" | sample (decimal value)
 +
|-
 +
| [[Binary numeral system|binary]] (<math>2</math>) || [[Percent sign|<syntaxhighlight lang="delphi" inline>%</syntaxhighlight>]] || <syntaxhighlight lang="delphi" inline>%1010</syntaxhighlight> (<math>10</math>)
 +
|-
 +
| octal (<math>8</math>)                            || [[&|<syntaxhighlight lang="delphi" inline>&</syntaxhighlight>]]            || <syntaxhighlight lang="delphi" inline>&644</syntaxhighlight> (<math>420</math>)
 +
|-
 +
| decimal (<math>10</math>)                        || ''none''                                                                  || <syntaxhighlight lang="pascal" inline>1337</syntaxhighlight> (<math>1337</math>)
 +
|-
 +
| [[Hexadecimal|hexadecimal]] (<math>16</math>)    || [[Dollar sign|<syntaxhighlight lang="delphi" inline>$</syntaxhighlight>]] || <syntaxhighlight lang="delphi" inline>$2A</syntaxhighlight> (<math>42</math>)
 +
|}
  
On older compilers, an integer was 16 bits, and represented values from 2**-15 through 2**15 -1, or -32,768 through 32,767.  A similar data type, [[word]], was sometimes used to define an unsigned integer (0..65,535). In such cases where the compiler used a 16-bit integer type, 32-bit integers would usually be expressed by the [[data type]] '''[[long]]''' or '''[[longint]]'''.
+
== characteristics ==
 +
It is guaranteed that all arithmetic operations in the range [[maxint|<syntaxhighlight lang="pascal" inline>−maxInt..+maxInt</syntaxhighlight>]] work accurately.
 +
An <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> [[Variable|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:
 +
<syntaxhighlight lang="pascal">
 +
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.
 +
</syntaxhighlight>
 +
Depending on the processor used, this ''may'' print:
 +
<syntaxhighlight lang="text">-9223372036854775808</syntaxhighlight>
 +
A quite unexpected result since [[Abs|<syntaxhighlight lang="pascal" inline>abs</syntaxhighlight>]] should in principle return a ''non-negative'' value, yet ''expectable'' since <syntaxhighlight lang="pascal" inline>pred(−maxInt)</syntaxhighlight> is evidently not in the <syntaxhighlight lang="pascal" inline>−maxInt..+maxInt</syntaxhighlight> range.
  
For x86 machines, an integer is generally defined as 32 bits, and encompasses the values of 2**-31 through 2**31 -1, or -2,147,483,648 .. 2,147,483,647.  The latter value is also defined as the constant [[maxint]]. The unsigned 32 bit integer type [[cardinal]] has a range of 0 through 2**32 -1 or 0 .. 4,294,967,295.
+
== application ==
 +
<syntaxhighlight lang="pascal" inline>Integer</syntaxhighlight> is the data of choice if arithmetic results have to be ''precise''.
 +
The data type [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>]] may provide “reasonable approximations”, but operations on <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> have to be exact (only guaranteed as long as it is in the <syntaxhighlight lang="pascal" inline>−maxInt..+maxInt</syntaxhighlight> range).
 +
Generally speaking <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> operations are also ''faster'' than if done in the domain of <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>.
  
On newer 64-bit processors an integer may be defined as a value of 2**-63 through 2**63-1, or -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,808.
+
The operators [[Div|<syntaxhighlight lang="pascal" inline>div</syntaxhighlight>]] and [[Mod|<syntaxhighlight lang="pascal" inline>mod</syntaxhighlight>]] only work on <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> values (the <syntaxhighlight lang="delphi" inline>math</syntaxhighlight> <syntaxhighlight lang="delphi" inline>unit</syntaxhighlight> provides the {{Doc|package=RTL|unit=math|identifier=fmod|text=<syntaxhighlight lang="delphi" inline>fMod</syntaxhighlight> <syntaxhighlight lang="pascal" inline>function</syntaxhighlight>}} and [[Operator overloading|overloads]] <syntaxhighlight lang="delphi" inline>mod</syntaxhighlight>).
 +
 
 +
== Free Pascal deviations ==
 +
The FPC does not have ''a single'' data type <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> but a host of <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> data types.
 +
An <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> literal such as <syntaxhighlight lang="pascal" inline>123</syntaxhighlight> possesses the data type of closest fitting range from the following table.
 +
{| class="wikitable" style="text-align: right; margin: auto;"
 +
|+ integer data types in FPC version 3.2.0
 +
! scope="column" | name (aliases)
 +
! scope="column" | smallest storable value
 +
! scope="column" | largest storable value
 +
! scope="column" | [[SizeOf|<syntaxhighlight lang="delphi" inline>sizeOf</syntaxhighlight>]]
 +
|-
 +
| style="text-align: left;" | {{Doc|package=RTL|unit=system|identifier=shortint|text=<syntaxhighlight lang="delphi" inline>shortInt</syntaxhighlight>}} ({{Doc|package=RTL|unit=system|identifier=int8|text=<syntaxhighlight lang="delphi" inline>int8</syntaxhighlight>}})
 +
| <syntaxhighlight lang="pascal" inline>-128</syntaxhighlight> (<math>-2^7</math>)
 +
| <syntaxhighlight lang="pascal" inline>127</syntaxhighlight> (<math>2^7-1</math>)
 +
| 1
 +
|-
 +
| style="text-align: left;" | {{Doc|package=RTL|unit=system|identifier=byte|text=<syntaxhighlight lang="delphi" inline>byte</syntaxhighlight>}} ({{Doc|package=RTL|unit=system|identifier=uint8|text=<syntaxhighlight lang="delphi" inline>uInt8</syntaxhighlight>}})
 +
| <syntaxhighlight lang="pascal" inline>0</syntaxhighlight> (<math>0</math>)
 +
| <syntaxhighlight lang="pascal" inline>255</syntaxhighlight> (<math>2^8-1</math>)
 +
| 1
 +
|-
 +
| style="text-align: left;" | {{Doc|package=RTL|unit=system|identifier=smallint|text=<syntaxhighlight lang="delphi" inline>smallInt</syntaxhighlight>}} ({{Doc|package=RTL|unit=system|identifier=int16|text=<syntaxhighlight lang="delphi" inline>int16</syntaxhighlight>}})
 +
| <syntaxhighlight lang="pascal" inline>-32768</syntaxhighlight> (<math>-2^{15}</math>)
 +
| <syntaxhighlight lang="pascal" inline>32767</syntaxhighlight> (<math>2^{15}-1</math>)
 +
| 2
 +
|-
 +
| style="text-align: left;" | [[Word|<syntaxhighlight lang="delphi" inline>word</syntaxhighlight>]] ({{Doc|package=RTL|unit=system|identifier=uint16|text=<syntaxhighlight lang="delphi" inline>uInt16</syntaxhighlight>}})
 +
| <syntaxhighlight lang="pascal" inline>0</syntaxhighlight> (<math>0</math>)
 +
| <syntaxhighlight lang="pascal" inline>65535</syntaxhighlight> (<math>2^{16}-1</math>)
 +
| 2
 +
|-
 +
| style="text-align: left;" | {{Doc|package=RTL|unit=system|identifier=longint|text=<syntaxhighlight lang="delphi" inline>longInt</syntaxhighlight>}} ({{Doc|package=RTL|unit=system|identifier=int32|text=<syntaxhighlight lang="delphi" inline>int32</syntaxhighlight>}})
 +
| <syntaxhighlight lang="pascal" inline>-2147483648</syntaxhighlight> (<math>-2^{31}</math>)
 +
| <syntaxhighlight lang="pascal" inline>2147483647</syntaxhighlight> (<math>2^{31}-1</math>)
 +
| 4
 +
|-
 +
| style="text-align: left;" | {{Doc|package=RTL|unit=system|identifier=longword|text=<syntaxhighlight lang="delphi" inline>longWord</syntaxhighlight>}} ({{Doc|package=RTL|unit=system|identifier=cardinal|text=<syntaxhighlight lang="delphi" inline>cardinal</syntaxhighlight>}}, {{Doc|package=RTL|unit=system|identifier=dword|text=<syntaxhighlight lang="delphi" inline>dWord</syntaxhighlight>}})
 +
| <syntaxhighlight lang="pascal" inline>0</syntaxhighlight> (<math>0</math>)
 +
| <syntaxhighlight lang="pascal" inline>4294967295</syntaxhighlight> (<math>2^{32}-1</math>)
 +
| 4
 +
|-
 +
| style="text-align: left;" | {{Doc|package=RTL|unit=system|identifier=int64|text=<syntaxhighlight lang="delphi" inline>int64</syntaxhighlight>}}
 +
| <syntaxhighlight lang="pascal" inline>-9223372036854775808</syntaxhighlight> (<math>-2^{63}</math>)
 +
| <syntaxhighlight lang="pascal" inline>9223372036854775807</syntaxhighlight> (<math>2^{63}-1</math>)
 +
| 8
 +
|-
 +
| style="text-align: left;" | {{Doc|package=RTL|unit=system|identifier=qword|text=<syntaxhighlight lang="delphi" inline>qWord</syntaxhighlight>}} ({{Doc|package=RTL|unit=system|identifier=uint64|text=<syntaxhighlight lang="delphi" inline>uInt64</syntaxhighlight>}})
 +
| <syntaxhighlight lang="pascal" inline>0</syntaxhighlight> (<math>0</math>)
 +
| <syntaxhighlight lang="pascal" inline>18446744073709551615</syntaxhighlight> (<math>2^{64}-1</math>)
 +
| 8
 +
|}
 +
The signed ranges are preferred (i.&#8239;e. as in the top/down order in the table), thus <syntaxhighlight lang="pascal" inline>123</syntaxhighlight> possesses the data type <syntaxhighlight lang="delphi" inline>shortInt</syntaxhighlight> even though it could be a <syntaxhighlight lang="delphi" inline>byte</syntaxhighlight>, too.
 +
 
 +
As of version 3.2.0, the data type {{Doc|package=RTL|unit=system|identifier=integer|text=<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>}} is simply an alias depending on the currently selected [[Compiler Mode|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 <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> provides.
 +
{| class="wikitable" style="margin: auto;"
 +
|+ the data type <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> in FPC version 3.2.0
 +
! mode
 +
! <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> is an alias for
 +
! value of <syntaxhighlight lang="pascal" inline>maxInt</syntaxhighlight>
 +
|-
 +
| [[Mode FPC|<syntaxhighlight lang="delphi" inline>{$mode FPC}</syntaxhighlight>]], [[Mode MacPas|<syntaxhighlight lang="delphi" inline>{$mode macPas}</syntaxhighlight>]] and [[Mode TP|<syntaxhighlight lang="delphi" inline>{$mode TP}</syntaxhighlight>]]
 +
| <syntaxhighlight lang="delphi" inline>smallInt</syntaxhighlight>
 +
| style="text-align: right;" |  <syntaxhighlight lang="pascal" inline>32767</syntaxhighlight>
 +
|-
 +
| ''all other available modes''
 +
| <syntaxhighlight lang="delphi" inline>longInt</syntaxhighlight>
 +
| style="text-align: right;" | <syntaxhighlight lang="pascal" inline>2147483647</syntaxhighlight>
 +
|}
 +
{{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 defines#Architecture defines|platform’s]] arithmetic logic unit’s word size (ALU) following {{gitlab|FPC|source|release_3_2_0/rtl/inc/systemh.inc#L390-L412|aliases are available}}.
 +
<syntaxhighlight lang="delphi">
 +
type
 +
{$ifDef CPU16} ALUSInt = smallInt; ALUUInt = word;  {$endIf}
 +
{$ifDef CPU32} ALUSInt = longInt;  ALUUInt = dWord; {$endIf}
 +
{$ifDef CPU64} ALUSInt = int64;    ALUUInt = qWord; {$endIf}
 +
</syntaxhighlight>
 +
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.&#8239;g. the x32-ABI uses 64‑bit ALU, but only 32‑bit pointers).
 +
Therefore if an <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> value is meant to be [[Typecast|typecasted]] to a [[Pointer|<syntaxhighlight lang="delphi" inline>pointer</syntaxhighlight>]], it recommended to use {{Doc|package=RTL|unit=system|identifier=ptruint|text=<syntaxhighlight lang="delphi" inline>ptrUInt</syntaxhighlight>}}.
 +
Note, the data type {{Doc|package=RTL|unit=system|identifier=nativeint|text=<syntaxhighlight lang="delphi" inline>nativeInt</syntaxhighlight>}} is in fact an alias for [[PtrInt|<syntaxhighlight lang="delphi" inline>ptrInt</syntaxhighlight>]] and {{gitlab|repository|FPC|release_3_2_0/rtl/inc/systemh.inc#L414-422|not related to <syntaxhighlight lang="delphi" inline>ALUSInt</syntaxhighlight>}}.
 +
 
 +
== see also ==
 +
* [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>]], the data type used to store/process (a subset of) rational numbers
 +
* [[gmp|GNU Multiple Precision Arithmetic Library]], precise mathematical operations on arbitrarily large values [if 8-Byte <syntaxhighlight lang="delphi" inline>int64</syntaxhighlight>/<syntaxhighlight lang="delphi" inline>qWord</syntaxhighlight> are not enough]
 +
 
 +
{{Data types}}

Latest revision as of 13:30, 5 April 2024

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