如何在c ++中存储一个非常大的数字

时间:2015-04-23 00:19:01

标签: c++

有没有办法在c ++中存储1000位数字?我尝试将它存储到unsigned long double,但它的类型仍然很大。

5 个答案:

答案 0 :(得分:4)

你可以在这里找到你的答案How to store extremely large numbers? GMP答案听起来是正确的,即这就是它对pi数字的作用https://gmplib.org/pi-with-gmp.html

答案 1 :(得分:1)

您必须自己实施或使用库。特别是我喜欢GMP:https://gmplib.org/,这是Big Int / Float的C实现,并且有C ++包装器

答案 2 :(得分:1)

为您的号码使用自定义类,如下所示:

#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.
    }

对于非常大的数字,只需将每个数字添加到向量。例如,如果数字为123456,则执行num.pushback();在这种情况下,按下所有数字1,2,... 6.您可以通过这种方式存储一个非常大的数字。

答案 3 :(得分:0)

你应该尝试这一个 http://sourceforge.net/projects/libbigint/

这样的大型图书馆。

你也可以使用boost。

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

也是最常见的一个是 https://gmplib.org/

答案 4 :(得分:0)

取决于用法。如果您需要对其进行计算,可能需要使用Big Int Library。如果没有且只有目标存储,则将其存储在一个数组中,每个数字存储在一个数组元素中。