0

I have to keep 10^31 range decimal data in an integer data type variable.

Which data type can hold this range's number?

Sudarshan
  • 765
  • 11
  • 25
  • 4
    GMP: https://gmplib.org/ You want to look for the term "arbitrary precision", and you'll find heaps of information, even here or [SO](http://stackoverflow.com/questions/2568446/the-best-cross-platform-portable-arbitrary-precision-math-library). – Jens Apr 09 '16 at 13:30
  • What are you trying to do exactly? – Matteo Italia Apr 09 '16 at 15:33
  • I was solving a programming problem, and I need to store the value and do some calculation. – Sudarshan Apr 10 '16 at 07:05

2 Answers2

5

No integer type defined by the C++ Standard can hold 1031. You need either

  • a 128-bit integer (range 1.7 × 1038). You have to resort to compiler-specific functionality for this (e.g. __int128_t in Clang and GCC).
  • or an arbitrary-precision integer class from a third-party library (checkout GMP).
emlai
  • 37,861
  • 9
  • 87
  • 140
  • Can I do it on java? – Sudarshan Apr 09 '16 at 13:43
  • Java doesn't have 128-bit primitive integers even as an extension so you have to use the arbitrary-precision integer class named `BigInteger`. – emlai Apr 09 '16 at 13:45
  • @Sudarshan: why did you tag your question C++ if you want a Java solution ? – Paul R Apr 11 '16 at 13:44
  • @Paul R : Basically, I was solving a problem in Google code jam. I was solving that problem with C++. But I could solve that with Java as well. If I could store the number in java, I could solve that with java. As I was trying with C++ I tagged my question with C++. – Sudarshan Apr 15 '16 at 10:50
0

No fundamental type integer type is guaranteed to hold that large a range. You can find the limits in climits. They're expressed as powers of 2, but it's easy to convert, 2^n is about 10^n/3.3, so long long will get you as far as 10^19 or so.

No floating point type can hold that range of values with whole number precision (they're too small).

You could make your own class with 14 bytes of precision (use 2 long long variables) which supports arithmetic operations, and internally manages arithmetic carry between the two variables.

The only alternative is to find a library which supports long, or infinite, precision.

QuestionC
  • 9,738
  • 3
  • 21
  • 40
  • @Sudarshan: what do you mean? If you encapsulate them in a class which overloads appropriately the arithmetic operators using it becomes almost completely transparent. – Matteo Italia Apr 09 '16 at 15:37