How to store a very large number in C ++

Is there a way to store a 1000 digit number in C ++? I tried to store it in unsigned long double, but it's still large in type.

+3


source to share


5 answers


You can find your answer here How to store very large numbers? The GMP answer sounds right, i.e. this is what it does with pi digits https://gmplib.org/pi-with-gmp.html



+3


source


You have to implement it yourself or use a library for this. In particular, I love GMP: https://gmplib.org/ , which is a C Big Int / Float implementation and has a C ++ wrapper



+1


source


Use your class for your number, for example:

#include <vector>
#include <iostream>

    class large_num {
     private:
        int digits;    // The number of digits in the large number
        std::vector<int> num;    // The array with digits of the number.
     public:
        // Implement the constructor, destructor, helper functions etc.
    }

      

For a very large number, just add each digit to the vector. For example, if a number, if 123456, you do num.pushback (); In this case, press all numbers 1,2, ... 6. This way you can store very large numbers.

+1


source


You should try this http://sourceforge.net/projects/libbigint/

great library for such things.

also you can use boost.

http://www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/boost_multiprecision/intro.html

also one of the most common https://gmplib.org/

0


source


Depends on the use. If you need to do calculations on it, probably go to the Big Int library. If not, and only the target is stored, store it in an array with each digit stored in one array element.

0


source







All Articles