C++ 中的大数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/238343/
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
Big number in C++
提问by AntonioCS
I am trying to place a big number in a C++ variable. The number is 600851475143
我试图在 C++ 变量中放置一个大数字。号码是600851475143
I tried unsigned long long int but got an error saying it the constant was too big. I then tried a bigInt library called BigInt -> http://mattmccutchen.net/bigint/
我尝试了 unsigned long long int 但得到一个错误,说常量太大了。然后我尝试了一个名为 BigInt 的 bigInt 库 -> http://mattmccutchen.net/bigint/
The problem is I can't compile the code as I get many errors regarding the lib.
问题是我无法编译代码,因为我收到了很多关于 lib 的错误。
undefined reference to `BigInteger::BigInteger(int)' <-- lot's of these.
对“BigInteger::BigInteger(int)”的未定义引用<--很多。
Here is my code so far:
到目前为止,这是我的代码:
#include "string"
#include "iostream"
#include "bigint/NumberlikeArray.hh"
#include "bigint/BigUnsigned.hh"
#include "bigint/BigInteger.hh"
#include "bigint/BigIntegerAlgorithms.hh"
#include "bigint/BigUnsignedInABase.hh"
#include "bigint/BigIntegerUtils.hh"
using namespace std;
int main() {
//unsigned long int num = 13195;
//unsigned long long int num = 600851475143;
BigInteger num = 13195;
int divider = 2;
//num = 600851475143;
while (1) {
if ((num % divider) == 0) {
cout << divider << '\n';
num /= divider;
}
else
divider++;
if (num == 1)
break;
}
}
If I put a smaller number and don't use the BigInt lib this program runs fine. Any help will be appreciated :D
如果我输入较小的数字并且不使用 BigInt 库,则该程序运行良好。任何帮助将不胜感激:D
回答by Martin York
You can specify an integer literal as long by the suffix L.
You can specify an integer literal as long long by the suffix LL.
您可以通过后缀 L
指定一个长整型文字。您可以通过后缀 LL 指定一个长整型文字。
#include <iostream>
int main()
{
long long num = 600851475143LL;
std::cout << num;
}
回答by Robert Gamble
The number is 600851475143 isn't too large for a long long int but you need to use the LL suffix when using a long long constants (ULL for unsigned long long int):
数字是 600851475143 对于 long long int 来说不算太大,但是在使用 long long 常量时需要使用 LL 后缀(ULL 表示 unsigned long long int):
unsigned long long int num = 600851475143ULL;
回答by artificialidiot
Raison d'etre of a big integer library is to represent integers which your language cannot handle natively. That means, you cannot even write it down as a literal. Probably, that library has a way to parse a string as a big number.
大整数库的存在理由是表示您的语言无法本地处理的整数。这意味着,你甚至不能把它写成文字。也许,那个库有一种方法可以将字符串解析为一个大数字。
回答by jakber
In a more general case when you cannot fit your number in a long long, and can live with the GNU LGPL license (http://www.gnu.org/copyleft/lesser.html), I would suggest trying the GNU Multiprecision Library (http://gmplib.org/).
在更一般的情况下,当您无法将您的数字放入 long long 并且可以使用 GNU LGPL 许可证(http://www.gnu.org/copyleft/lesser.html)时,我建议您尝试使用 GNU Multiprecision Library (http://gmplib.org/)。
It is extremely fast, written in C and comes with a very cool C++-wrapper-library.
它非常快,用 C 编写,并带有一个非常酷的 C++-wrapper-library。
回答by coppro
If you are getting undefined reference errors for the bignum library, you probably didn't link it. On Unix, you will have to pass an option like -lbigint. If you are using an IDE, you will have to find the linker settings and add the library.
如果您收到 bignum 库的未定义引用错误,您可能没有链接它。在 Unix 上,您必须传递一个像 -lbigint 这样的选项。如果您使用的是 IDE,则必须找到链接器设置并添加库。
As for the numbers, as has already been said, a natural constant defaults to int type. You must use LL/ll to get a long long.
至于数字,如前所述,自然常量默认为 int 类型。您必须使用 LL/ll 来获得 long long。
回答by Dima
The first thing to do in this case is to figure out what is the largest number that you can fit into an unsigned long long. Since it is 64 bit, the largest number would be 2^64-1 = 18446744073709551615, which is larger than your number. Then you know that you are doing something wrong, and you look at the answer by Martin York to see how to fix it.
在这种情况下要做的第一件事是找出可以放入 unsigned long long 的最大数字是多少。由于它是 64 位,因此最大的数字将是 2^64-1 = 18446744073709551615,这比您的数字大。然后你知道你做错了什么,你看看马丁约克的答案,看看如何解决它。
回答by Martin Beckett
Is there a bigint lib to link in or a bigint.cpp to compile?
是否有要链接的 bigint lib 或要编译的 bigint.cpp?
回答by siddhusingh
Try this one. Basically you can have your own custom class which uses linked list to store the number of infinite size. ( RAM is the restriction ) Try this one https://mattmccutchen.net/bigint/
试试这个。基本上你可以拥有自己的自定义类,它使用链表来存储无限大小的数量。(RAM 是限制)试试这个 https://mattmccutchen.net/bigint/
回答by Muricula
For anyone else having problems with this library five years after this question was asked, this is the answer for you. You cannot just compile your program, it will fail to link with an ugly impenetrable error! This library is a collection of c++ files which you are supposed to compile to .o files and link against. If you look at the output of the make file provided with the sample program you will see this:
对于在提出这个问题五年后对这个库有问题的其他人,这就是你的答案。你不能仅仅编译你的程序,它会因为一个丑陋的难以理解的错误而无法链接!这个库是一个 C++ 文件的集合,你应该将它们编译为 .o 文件并进行链接。如果您查看示例程序提供的 make 文件的输出,您将看到:
g++ -c -O2 -Wall -Wextra -pedantic BigUnsigned.cc
g++ -c -O2 -Wall -Wextra -pedantic BigInteger.cc
g++ -c -O2 -Wall -Wextra -pedantic BigIntegerAlgorithms.cc
g++ -c -O2 -Wall -Wextra -pedantic BigUnsignedInABase.cc
g++ -c -O2 -Wall -Wextra -pedantic BigIntegerUtils.cc
g++ -c -O2 -Wall -Wextra -pedantic sample.cc
g++ sample.o BigUnsigned.o BigInteger.o BigIntegerAlgorithms.o BigUnsignedInABase.o BigIntegerUtils.o -o sample
Replace sample
with the name of your program, paste these lines in a makefile or script, and away you go.
替换sample
为您的程序名称,将这些行粘贴到 makefile 或脚本中,然后就可以了。