I find numeric limits of long long int insufficient. Is there any other numeric type in C++ like big integer in java?

I stumbled upon this question recently and didn't knew how to tackle it..... https://blog.codechef.com/2009/07/02/tutorial-for-small-factorials/

  • 3
    No there isn't any standard bigint type. There are libraries supporting such thogh. – πάντα ῥεῖ Jan 17 '16 at 19:31
  • There are a few different "big int" options for C++. Maybe look at this one: gmplib.org – Galik Jan 17 '16 at 19:46
  • Consider double or decimal. – Zdenek Jan 17 '16 at 21:05
  • @Zdenek: If long long int is too small, how would using a double help? It only supports all the integers up to 2^53 - 1 (after that, you start losing precision) --- which is less than the minimum that a long long int must support. And that's assuming double is IEEE 64-bit floating-point format (they could be 32-bit and non-IEEE, for example). Also, there is no decimal type in C. – Tim Čas Aug 14 '17 at 15:49
  • You are assuming that OP needs to avoid underflow. That may not be the case. Any float can represent a larger span of numbers than an integer which may be what he needs. You are of course right in that a 64-bit float has only 53 bits of mantissa. – Zdenek Aug 15 '17 at 19:01

unsigned long long int is biggest integer type in standard C++ ( it can hold numbers from 0 to 18 446 744 073 709 551 615 ), if you want bigger ones you may need to search for some bignum libraries like this: http://www.ttmath.org/

  • 4
    The standard doesn't tell how big (or small) are the numbers it can hold. This is implementation dependent and shall be verified with std::numeric_limits<unsigned long long>::max() – Christophe Jan 17 '16 at 19:54
  • 1
    @Christophe - the standard does require each integer type to cover a minimum range. Essentially, unsigned long long must be at least 64 bits wide. – Pete Becker Jan 18 '16 at 2:52
  • @PeteBecker Looking while this is true for most implementation, C++ 3.9.1 (pt.2 and 3) does not guarantee 64 bits. Can you provide any reference for this claim ? – Christophe Jan 18 '16 at 7:44
  • 1
    @Christophe but still, unsigned long long int will be biggest integer value possible to store – encoreleeet Jan 18 '16 at 12:03
  • 2
    @Christophe - this "claim" comes in through the C11 standard, section – Pete Becker Jan 18 '16 at 14:31

Type Name - long long

Bytes - 8

Range of Values - –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Ref: Microsoft

  • 4
    The OP didn't specify the compiler. This is implementation dependent. – Christophe Jan 17 '16 at 19:55

You can use the C++ library <boost/multiprecision/cpp_int.hpp> for such problems.

For example:

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
namespace mp = boost::multiprecision;
using namespace std;
int main()
    mp::cpp_int f = 1;
        f *= i;
    cout << "100! = " << f << '\n';

This will give the output

100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

To know more about C++ boost library, you can refer here and here

Your Answer

