In some of the programming languages, typically c , c++ , and java , the integer data types are int
, , short
and long
. The data type int
is usually 32 bits. The data type short
is typically smaller than the data type int
and the data type long
is typically larger than the data type int
.
The first bit of an integer data type is the sign bit . A negative number starts with '1' and a non-negative number starts with '0'.
Therefore, a 32-bit data type typically stores numbers in the range -(2^31)
a (2^31 - 1)
.
- Why can't we have a data type of unlimited size, so that we can store an unlimited range of numbers?
The limitations you speak of have to do with the way programming languages are designed.
A programming language arises from the convenience of avoiding the use of assembly or machine language for developments that do not require its express use.
However, even if we try to avoid the use of assembler, the programming language has a series of limitations imposed by the processor that is going to execute the code.
Current processors are 64-bit, that is, they are capable of natively handling 64-bit information (in some cases they can even handle 128-bit), but beyond that the system does not have any hardware that allows it to handle values bigger.
It's because of this processor dependency that some languages, like C and C++, don't really have a fixed type for data as well. In fact, if you consult the latest draft of C++, you will see that the standard tells you about a minimum size for each data type. The maximum is normally given by the architecture of the processor.
If someone needs a numeric data type that uses more bits than the processor supports, then they will have to program that support in software. It is not an impossible operation but it is cumbersome.
If you are not willing to program all that logic, you will have to find a library that provides this functionality.
The visible Universe has 10^80 particles. Therefore, to represent any material value in this Universe you need at most 80 decimal digits long.
For any real-life problem (build a bridge, erect a building, design an engine) 4 decimal digits of precision is enough.
Until the middle of the 20th century, humanity managed with calculation rules (three digits of maximum precision) for all its engineering works.
So the reason is: with the types of data we have, it's enough for any practical application.
Building arbitrary precision computers adds cost and complexity to the design without yielding any benefits.
Arguing on this question:
Why can't we have a data type of unlimited size, so that we can store an unlimited range of numbers?
-A. Hardware architecture.
-B. Language Design.
A. When designing a programming language, we always depend on the hardware on which it is executed; and no hardware has Unlimited Processor, Memory, Storage; this is our limitation and we must work on what currently exists or create it.
B. When a language is designed. what is sought is to avoid machine language, or to avoid having to program at such a low level, then the design of the language will depend directly on the hardware and avoid losing control of it.
Because your question is very broad:
Then depending on the design of the Language (which are many) and on which Hardware it runs, you will be able to create primitive data of greater or lesser size:
Like for example programming C for a 32 bit and a 64 bit processor.
Then we have a single language with different capabilities depending on the platform. or we can also have specific languages for certain platforms or hardware.
Response:
Because there is the Overflow and you have to avoid it. Link Loss of control of hardware resources.
Now your question is very broad since you are NOT giving us a precise scenario , exactly where you want to know why we cannot store an unlimited range of numbers since the answer can vary from the language, its design and the hardware in which it is executed.
As a closing: But I assure you that the day we can store data in the vacuum of space then we could say that we have infinite storage space for your numbers.
Expanding on the previous answers I would like to say that in C# and Java you can work with the BigInteger data type , which will allow you to work with immensely large numbers. BigInteger overloads standard numeric operators to allow you to perform basic math operations like addition, subtraction, division, multiplication, and unary negation. You can also use the standard numeric operators to compare two BigIntegers with each other, and it also supports bitwise operators such as Or , XOr , And , shift left , and shift right .
In Java , there is also the BigDecimal data type which is used to represent decimal numbers with an immensely high precision.