我知道的最大的正整数数据类型是unsigned long long。有没有办法在C ++中处理2 ^ 9000000的整数。我在代码块中使用了gcc编译器,但也可以在Visual Studio中使用。
您需要某种BigInt库。我个人更喜欢boost.multiprecision,因为它包含普通代码编写人员所需的大多数实用程序。
根据您实际想要使用的东西,有两种明显的类型需要考虑。
boost::multiprecision::cpp_int
,它将通过分配大约900万位数据(略大于1兆字节)并存储整个数字来存储2 ^ 9000000的数字。在那儿。cpp_bin_float
后端之类的方法,尽管为此您可能必须定义自己的模板,因为预烘焙的版本可能不够大。对于后者,可能的例子:
using namespace boost::multiprecision;
using my_big_float = number<backends::cpp_bin_float<100, backends::digit_base_2, void, boost::int32_t, -9000000, 9000000>, et_off>;
//Defines the number to have 100 bits of precision, an exponent range from negative
//nine million to positive nine million, and uses 32-bit ints for the internal representation
//of the exponent. Allocator is void (because we don't need it) and et_off just turns off
//expression templates, which we don't need
int main() {
my_big_float fl = 5;
fl = pow(fl, my_big_float{900007});
std::cout << fl << std::endl;
}
7.88302e+629077
我不知道您的用例是什么,但是我猜想后者对于您的用例会比前者更好。您必须自己决定情况是什么。