r/ProgrammerTIL • u/BrainFRZ • Nov 04 '17
Other Language [General] TIL The highest number most programs might handle is about 41373247548 digits.
Almost all programs are written in languages that have a max limit for integers of 263 -1, and we can reach 264 -1 if we make sure it's positive. That's 18446744073709551614, which is 20 digits. Some languages have built-in arbitrary precision number systems that are based on string representations (meaning the number is stored as text). Unfortunately, these languages generally have a string length limit of 231 -1, meaning that's the largest number of digits we can get.(*) That's really cool, and can give us 2147483647 digits.
Then comes GMP. GMP is also useable in most programming languages, and some use them by default, such as Haskell. You can't have a whole number larger than 237 -64 bits (that's over 17GB to hold the one number). So, that value as an actual number is 2137438953408. Unfortunately, I don't have 17GB RAM on my laptop, so I can't calculate it. It's also a little frustrating, because it would only take about 37 calculations to solve, so it'd be done in milliseconds. Fortunately, we have the change of base formula. We can calculate that 2137438953408 is approximately 1041373247548.47235.
Therefore, although I didn't learn what that number was, we know it's about 41373247548 digits. The number of digits is 11 digits long.
(*) Of course every language can do what it wants, but address space to store the list seems to be the general problem. Here's the same problem in Python.
28
u/matessim Nov 04 '17 edited Nov 04 '17
I wouldn't say it's a 'problem', it's a characteristic of the CPU Design and supported sizes, and there are very simple workarounds, There is also no single opcode to calculate log base 20, doesn't mean you can't do it (or that it's even a problem, it's quite simple) , Java has BigInteger, and as you linked, Python has automatic casting built-in to the various decimal types. You could make a Integer that's 4KB in C or C++ also (either implement yourself the various methods such as addition, multiplication etc) like you said with GMP.
The fact we don't have opcodes for 8KB Integers is mostly because we don't need them (or more precisely, it's not a big enough issue for enough use cases, there are definitely problems where you can benefit from being able to multiply 2048 bit numbers in one cycle) ,you do have SSE/SSE2 etc opcodes that work on very large registers (256 bits for example), but not having a single opcode to do the work doesn't make it a 'problem' beyond being slower, it's still very easy to do and well supported in the languages, pretty much every language has 'boxed' integer types to support larger than single-opcode addressable platform operations support.
If you're just counting the human body has about 3 * 1013 germs in it's body, log2 that's around 244, still very far off, there's not much things who's count is that big (Money, people, any real world object you'll need to count really, excluding things like the number of atoms in the universe, whose magnitude is around 270~ish bits) Again, There are many use cases for large numbers, but it's not a 'problem', it just doesn't happen in one cycle on a x86-64 processor.
8
u/CptCap Nov 04 '17 edited Nov 04 '17
I am not aware of any language that does that. Chaining (binary) integers is not that much harder and a lot faster.
This statement makes no sense.
"a calculation" doesn't translate to a computation time like this. Doing a modexp on a 17GB number is a shitload of computation and a lot of data to transfer from and to RAM. This "calculation" will take a lot more than a few millisecond.
Try to type
9**84684843in python and see how long it takes.7
u/sim642 Nov 04 '17
No language implements bigints via characters in strings because it's so horrendously inefficient, so any claim about that is absolutely worthless.
GMP is only one library which some languages use internally but that definitely does not cover most languages or libraries. Some implement basic bigints on their own. GMP is definitely not the only library that can be and is used for bigints.
So really this is just a random unrelated facts that don't accurately represent the generality. In theory there is no limit anyway. In practice most programs don't use bigints but just the native integers which are nowhere that big.
4
u/[deleted] Nov 04 '17
Except Python where the integers can be as big as you like. And C/C++ (where
unsigned long longgoes up to 264 - 1 - or if you use gcc, you can get up to 2128 - 1 withuint128_t). And Java, which has BigDecimal and BigInteger. And Perl, which has Math::BigInt.Indeed, of all the languages I know, only Javascript restricts integers to being less than 263.
1
u/Eric2416 Nov 04 '17
This man's a genius