2.8: Integer Data Type
- Page ID
An integer data type represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set of integer sizes available varies between different types of computers and different programming languages.
The integer data type basically represents whole numbers (no fractional parts). The integer values jump from one value to another. There is nothing between 6 and 7. It could be asked why not make all your numbers floating point which allow for fractional parts. The reason is threefold. First, some things in the real world are not fractional. A dog, even with only 3 legs, is still one (1) dog not ¾ of a dog. Second, the integer data type is often used to control program flow by counting, thus the need for a data type that jumps from one value to another. Third, integer processing is significantly faster within the CPU than is floating point processing.
The integer data type has similar attributes and acts or behaves similarly in all programming languages that support it.
|C++|| ||16 bits / 2 bytes||-32,768 to32,767|
|C++|| ||varies||depends on compiler|
|C++|| ||32 bits / 4 bytes||-2,147,483,648 to 2, 147,483,647|
|C++|| ||64 bits / 8 bytes||−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|C#|| ||16 bits / 2 bytes||-32,768 to32,767|
|C#|| ||32 bits / 4 bytes||-2,147,483,648 to 2, 147,483,647|
|C#|| ||64 bits / 8 bytes||−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|Java|| ||16 bits / 2 bytes||-32,768 to32,767|
|Java|| ||32 bits / 4 bytes||-2,147,483,648 to 2, 147,483,647|
|Java|| ||64 bits / 8 bytes||−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|Python|| ||no limit|
|Swift|| ||varies||depends on platform|
|Swift|| ||32 bits / 4 bytes||-2,147,483,648 to 2, 147,483,647|
|Swift|| ||64 bits / 8 bytes||−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
For C++ and Swift the size of a default integer varies with the compiler being used and the computer. This effect is known as being machine dependent. These variations of the integer data type are an annoyance for a beginning programmer. For a beginning programmer, it is more important to understand the general attributes of the integer data type that apply to most programming languages.
Math.round() function may be used to return the value of a number rounded to the nearest integer.
Python 3 integers are not limited in size, however,
sys.maxsize may be used to determine the maximum practical size of a list or string index.
- machine dependent
- An attribute of a programming language that changes depending on the computer’s CPU.
Wikipedia: Integer (computer science)