I wanted a datatype which can hold as large as 10^21. After searching enough, I got unsigned long long as holding somewhere around 18*10^18 which is still not enough. What shall I do ?
Asked
Active
Viewed 828 times
0
-
Depends on your application. Is integer-level precision required? – Jackson May 08 '18 at 04:07
-
1use `std::numeric_limits` – Joseph D. May 08 '18 at 04:08
-
Aaah. Long and double will not work. So, I need something else. – asn May 08 '18 at 04:09
-
Well, what was that ? Went straight above my head !! – asn May 08 '18 at 04:11
-
@Jacob what about to implement you own class to keep it? Or you want to find an standart solution? – Vladimir Ch. May 08 '18 at 04:18
-
Both ways will work but the standard one will be handy and simpler, I hope ! – asn May 08 '18 at 04:20
-
Try this: https://www.boost.org/doc/libs/1_62_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html Boost has types up to 1024 bit size. – Vladimir Ch. May 08 '18 at 04:23
-
Well, there what does "unsigned i" mean in the loop at the end of the page ? – asn May 08 '18 at 04:26
-
There's always [The GNU Multiple Precision Arithmetic Library](https://gmplib.org/). – Jesper Juhl May 08 '18 at 05:34
2 Answers
3
g++
, clang++
, and most platforms support __uint128
and __int128
, which hold 128 bits. You can use them like normal integers, but they tend to be a bit less compatible with the standard library.
This can hold numbers up to 2^128 ~ 10^38
You could alternatively use double
or float
, but you will lose precision.
Or you could use a BigInt library, which allows arbitrary precision integers.

Kostas
- 4,061
- 1
- 14
- 32
-
-
@Jacob Like normal integers. But bigger. Note that `uint` means `unsigned int`. – Kostas May 08 '18 at 04:12
-
-
I hope that I will have to just replace "int" with "_uint128" OR something else needs to be added ? – asn May 08 '18 at 04:22
-
1@Jacob Yes, this should be fine. Actually after a bit more research, your processor must have support for 128 bit long long. I think most modern processors do though, so you should be fine. – Kostas May 08 '18 at 04:23
-
1Is there a shorter sized int than this one if I want to be memory specific !! – asn May 08 '18 at 04:27
-
Is there any difference between adding #include
, then using int128_t AND the way u provided (__unit128) ?? – asn May 08 '18 at 04:31 -
1@Jacob Unfortunately ints go in multiples of 2. But if you use a BigInt Library, it might allocate memory better (even though the way memory is usually expanded is in multiples of 2, so probably not). – Kostas May 08 '18 at 04:32
-
@Jacob I didn't know about boost. That's good to know. I suppose it's similar but don't we all love built-in stuff? – Kostas May 08 '18 at 04:33
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170579/discussion-between-gill-bates-and-jacob). – Kostas May 08 '18 at 04:34
0
We used Magma for these sorts of numbers: https://en.wikipedia.org/wiki/Magma_(computer_algebra_system) http://magma.maths.usyd.edu.au/magma/
Not sure if they have a C++ library but something to look into. The numbers these things can hold were pretty impressive.

solarflare
- 423
- 3
- 14