1

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/

Yash Kant
  • 458
  • 1
  • 6
  • 11
  • 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: https://gmplib.org/ – Galik Jan 17 '16 at 19:46
  • Consider `double` or `decimal`. – Zdenek Jan 17 '16 at 21:05
  • 1
    @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

3 Answers3

12

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/

encoreleeet
  • 374
  • 2
  • 9
  • 8
    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::max()` – Christophe Jan 17 '16 at 19:54
  • 2
    @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 02: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 07:44
  • 1
    @Christophe but still, unsigned long long int will be biggest integer value possible to store – encoreleeet Jan 18 '16 at 12:03
  • @encoreleeet this i fully agree ! My only concern is about the max value, not its type – Christophe Jan 18 '16 at 13:30
  • 2
    @Christophe - this "claim" comes in through the C11 standard, section 5.2.4.2.1. – Pete Becker Jan 18 '16 at 14:31
  • @PeteBecker ok, [point taken !](http://stackoverflow.com/questions/10053113/is-c11s-long-long-really-at-least-64-bits): I did only check in C++ std. But I'd still advise to use `numeric_limits` instead of using some hard coded value ;-) – Christophe Jan 18 '16 at 18:05
6

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

rbashish
  • 2,073
  • 2
  • 24
  • 35
3

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

For example:

#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using namespace std;
int main()
{
    cpp_int f = 1;
    for(int i = 1; i <= 100; i++)
        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