0

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 ?

asn
  • 2,408
  • 5
  • 23
  • 37

2 Answers2

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
  • Thanks ,but how shall I use these in my program ? – asn May 08 '18 at 04:07
  • @Jacob Like normal integers. But bigger. Note that `uint` means `unsigned int`. – Kostas May 08 '18 at 04:12
  • Is it applicable for some specific processors ? – asn May 08 '18 at 04:17
  • 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
  • 1
    Is 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