C++ 极大整数的变量类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15400031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What variable type for extremely big integer numbers?
提问by user1815324
I've tried using
我试过使用
long long int
But it wont work for numbers like 3141592653589793238462643383279502884197169399375, I need this up to 10 ^ 80. Any idea? Let me know. Thanks alot.
但它不适用于像 3141592653589793238462643383279502884197169399375 这样的数字,我需要最多 10 ^ 80。知道吗?让我知道。非常感谢。
回答by zwol
You can't use any built-in integer type for this. You need a "multiple precision integer" aka "bignum" library. For C++, I would try Boost.Multiprecisionfirst, but be aware that Boost can be considerably more trouble than it is worth, particularly if the module you're using has any shared library (aka DLL) component. The other obvious choice is GNU MP. It only has a C interface, but it is well-maintained, reliable, fast, and very popular (in fact, it appears that Boost.MP is "just" a C++ wrapper for it!)
您不能为此使用任何内置整数类型。您需要一个“多精度整数”又名“bignum”库。对于 C++,我会先尝试Boost.Multiprecision,但要注意 Boost 可能比它的价值要麻烦得多,特别是如果您使用的模块具有任何共享库(又名 DLL)组件。另一个明显的选择是GNU MP。它只有一个 C 接口,但它维护良好、可靠、快速且非常流行(实际上,Boost.MP 似乎“只是”它的 C++ 包装器!)
WARNING: You might want a bignum library because you are trying to implement one of the cryptographic primitives that uses huge numbers, like RSA. Do not do this. The generic bignum libraries are not safe for cryptographic use, and even if they were, there would still be dozens of subtle mistakes you can make that would ruin your security. Use a well-tested cryptography library instead; for C++ I recommend Botan.
警告:您可能需要一个 bignum 库,因为您正在尝试实现使用大量数字的加密原语之一,例如RSA。 不要这样做。通用 bignum 库对于加密使用并不安全,即使它们是安全的,您仍然会犯许多细微的错误,这些错误会破坏您的安全性。改用经过良好测试的加密库;对于 C++,我推荐Botan。