Handle arbitrary length integers in C++(在 C++ 中处理任意长度的整数)
问题描述
有人能告诉我一个好的 C++ 库,用于处理(执行操作等)任意大的数字(它也可以是处理任意精度浮点数的库,但处理整数更重要)?
Can someone tell me of a good C++ library for handling (doing operations etc...) with arbitrarily large numbers (it can be a library that handles arbitrary precision floats too, but handling integers is more important)?
请仅参考您使用的库,并告诉我您是如何设法设置和获取它的,可能是一个非常简约的示例或其他东西(基本上,如果提到的库缺乏良好的文档,请提供您自己的一些输入).
Please only refer to libraries that YOU used and tell me how did you managed to set it up and pick it up, maybe with a very minimalistic example or something (basically if the mentioned library lacks good documentation provide some input of your own).
作为记录,我在 x64 机器上使用 Windows 7,CodeBlocks 作为我的 IDE,最新的 MinGW 作为编译器.
For the record I'm using Windows 7 on an x64 machine, CodeBlocks as my IDE, and the latest MinGW as the compiler.
我尝试过的库:
vlint(没有足够的操作,不过对于小东西也可以)
vlint (not enough operations, works fine for small stuff though)
bigint(易于设置、编译错误且文档不多(可能从中衍生错误))
bigint (easy to set it up, compile errors and not much documentation (from which errors might be derived))
ttmath(看起来很有希望,编译了一些 BIG 示例程序并在一些修复后运行,因为编译错误,由于几乎没有文档,语法难以理解)
ttmath (seemed promising, compiled some BIG example programs and ran after some fixes because of compiling errors, incomprehensible syntax because of virtually no documentantion)
gmp(甚至无法设置)
gmp (couldn't even set it up)
附言删除了问题的咆哮部分",这基本上解释了为什么我要问一些在 Stackoverflow 上被问了很多次的东西,这样人们就会把它读到最后.
p.s. Removed the 'rant part of the question' that basically explained why I'm asking something that was asked a lot of times on Stackoverflow so people would read it to the end.
-->更新
所以我选择的答案不是我最初问题的直接答案,但对我解决这个问题有很大帮助,我将发布我的一些发现来帮助像我这样的其他 c++ 新手开始使用非常大的数字没有像我在一个简单的一步一步的微型指南中那样与图书馆苦苦挣扎的日子.
So i picked an answer that wasn't a direct answer to my initial question but helped me a lot to solve this and i will post some of my findings to help other c++ newbies like me to get started working with very big numbers without struggling with libraries for days like i did in an easy step by step micro-guide.
我正在使用的东西(请记住这一点以遵循指南):
Stuff i was using (keep this in mind to follow the guide):
Windows 7 Ultimate x64
Amd k10 x64(有些库不能使用它,有些库的行为会有所不同,有些库是为 amd k10 定制的,所以这不仅可以帮助您使用 i 库使用过,但也可能与其他人一起使用)
Amd k10 x64 (some libraries won't work with this, others will behave differently, others are custom taylored to amd k10 so this won't only help you with the library i used but possibly with others too)
Code::Blocks 10.05 不包含MinGW的版本,文件名codeblocks-10.05-setup.exe"(安装在C:Program Files (x86)CodeBlocks)
Code::Blocks 10.05 the version without MinGW included, file name "codeblocks-10.05-setup.exe" (installed on C:Program Files (x86)CodeBlocks)
MinGW 包 (binutils-2.15.91-20040904-1.tar.gz gcc-core-3.4.2-20040916-1.tar.gz gcc-g++-3.4.2-20040916-1.tar.gz mingw-runtime-3.11.tar.gz w32api-3.8.tar.gz) 在 C:MinGW 上提取
MinGW packages (binutils-2.15.91-20040904-1.tar.gz gcc-core-3.4.2-20040916-1.tar.gz gcc-g++-3.4.2-20040916-1.tar.gz mingw-runtime-3.11.tar.gz w32api-3.8.tar.gz) extracted on C:MinGW
TTMath 0.9.2 文件名ttmath-0.9.2-src.tar.gz"解压后将文件夹ttmath"复制到文件夹C:CPPLibs"(这是我将 C++ 库放入的文件夹)
TTMath 0.9.2 file name "ttmath-0.9.2-src.tar.gz" unzipped and copied the folder "ttmath" to the folder "C:CPPLibs" (which is the folder where i put my c++ libraries into)
如何进行设置
转到 Code:Blocks > Settings > Compiler and Debugger(我的编译器在此处自动检测到.如果您没有发生这种情况,在Selected Compiler"中选择GNU GCC编译器"并单击设置为默认值",然后在编译器安装目录"上的工具链可执行文件"上,您可以选择编译器的安装目录或尝试自动检测",然后在C++编译器"上排序,选择或写入mingw32-g++.exe".如果您遇到这种情况,只需执行此操作,在Selected Compiler"上选择GNU GCC Compiler"并单击Set as Default").
Go to Code:Blocks > Settings > Compiler and Debugger (My compiler was detected automatically here. If this doesn't happen with you, on "Selected Compiler" select "GNU GCC Compiler" and click "Set as Default" then on "Toolchain Exectables" on "Compilers installation directory you can choose the installation directory of the compiler or attempt to auto-detect" and with that sorted on "C++ Compiler" select or write "mingw32-g++.exe". If this happens to you just do this, on "Selected Compiler" select "GNU GCC Compiler" and click "Set as Default").
不离开代码:块>设置>编译器和调试器"并整理好上述内容,转到搜索目录",然后编译器"单击添加"并选择存储库的文件夹或者您将ttmath"文件夹(在我的情况下为 C:CPPLibs)放在哪里,然后转到Linker"并做同样的事情.
Without leaving "Code:Blocks > Settings > Compiler and Debugger" and with the above sorted out, go to "Search Directories" and then "Compiler" click "Add" and choose the folder where you store your libraries or where you put your "ttmath" folder (in my case C:CPPLibs) then go to "Linker" and do the same thing.
要开始使用ttmath"库进行编码,您必须将此行 #include <ttmath/ttmath.h>
放在 main 函数之前(注意: 如果你使用 64 位系统,如果你不把这行 #define TTTMATH_DONT_USE_WCHAR
BEFORE 这行 #include <ttmath/ttmath.h>
,我一直在为这个废话苦苦挣扎,直到我找到了其他也在苦苦挣扎的人找到并发布在网上的修复程序,它对我有用)ps我认为它仅适用于 64 位系统,但如果您确实因为包含ttmath.h"头文件而出现错误,那很可能是因为这个.
To start coding with the "ttmath" library you have to put this line #include <ttmath/ttmath.h>
before the main function (NOTE: If you use a 64bit system you WILL get a lot of errors if you don't also put this line #define TTMATH_DONT_USE_WCHAR
BEFORE this line #include <ttmath/ttmath.h>
, i was struggling with this crap until i found the fix that some other guy that was also struggling found and posted on the web and it worked for me) p.s. i think it's only for 64bit systems but if you do get errors just because of including the "ttmath.h" header file it's most likely because of that.
声明具有大整数值的变量必须这样做:ttmath::UInt<n>a,b,c;
其中a,b,c"是您的变量,n"是您可以以这种形式存储在变量中的数字的大小2^(32*n)-1" 用于 32 位系统,这种形式 "2^(64*n)-1" 用于 64 位系统
Declaring variables that will have big integer values has to be done like so: ttmath::UInt<n> a,b,c;
where "a,b,c" are your variables and "n" is the size of the numbers you can store in the variables in this form "2^(32*n)-1" for 32bit systems and this form "2^(64*n)-1" for 64bit systems
给变量赋值如果你这样做a = 333;
(代替333的数字大于long int"标准数据在 c++ 上键入)它不会编译,因为将值分配给这样的变量独立于您之前指定的大小整数可以与 c++ 上的long int"标准数据类型一样大(我自己想出了这个),困难的方式),即使您使用较小的值并且编译正常,然后您运行程序并尝试向该变量写入比提到的long int"标准数据类型的数字更大的数字可以处理,那么您的数学将是错误的,所以请注意:要将值分配给变量正确的方式,您必须像这样分配它 a = "333";代码>(是的,我知道您几乎是以这种方式将其视为字符串,但它会很好地执行操作而没有任何问题,如果您决定计算"变量,它将永远不会是指数或科学记数法的结果,就像您使用标准整数数据类型获得的结果一样,无需加上一些额外的语句"来显示恰到好处的数字)
Assigning values to variables if you do this a = 333;
(and the number in place of 333 is bigger than the "long int" standard data type on c++) it won't compile because assigning values to variables like this independently of the size you specified earlier the integer can be just as big as the the "long int" standard data type on c++ (i figured this one on my own, the hard way), also even if you use a value that is smaller and it compiles alright and then you run your program and it tries to write to this variable a bigger number than the number that the mentioned "long int" standard data type can handle, then your math is going to be wrong so watch this: to assign a value to a variable the right way you have to assign it like so a = "333";
(yes i know you are pretty much treating it as a string this way but it will do operations just fine with no problems whatsoever and if you decide to "cout" the variable it will never be an exponential or scientific notation result like you get using standard integer data types without being coupled with some 'extra statements' to display the number just right)
附言使用这个简单的规则来处理整数和这个库,我用一个简单的程序(编码大约需要 3 分钟)在 15 到 20 秒内计算出第 100.000 个数字的斐波那契数字,并且这个数字占用了 3 页,所以除了实用之外图书馆一旦你了解它是如何工作的(你以前几乎没有任何帮助,ttmath 网站的一些示例非常具有误导性,但现在你确实有一些帮助)它看起来也很有效,我确认第 100.000 个数字是可能是对的,因为我将大小(n")从 10000 增加到 50000,并且数字保留了大小,并且初始和最终数字相同.这是我使用的源代码,我使用一个非常大的数字作为整数大小只是为了测试,我实际上并没有费心去看看程序开始做错事的长度,但我知道最长可达第 10.000 个斐波那契数不会超过我定义的长度,因为在此之前我让程序计算"每个结果,直到它达到第 10.000 个并且它一直在增长.在我暂停程序之前,我还检查了序列的第一个数字,我看到数字增长"并确认了序列的第一个斐波那契数字,它们是正确的.注意:此源代码只会显示您想知道的斐波那契数列的编号,如果您取消注释行,它只会显示增长"的数字.
p.s. Using this simple rules to work with integers and this library i computed fibonacci numbers up to the 100.000th number with a simple program (that took like 3min to code) in 15 to 20 seconds and the number occupied like 3 pages so besides being a practical library once you get to know how it works (that you had virtually no help before, some samples of the ttmath website are pretty misleading, but now you do have some help) it also seems pretty efficient, i confirmed that the 100.000th number is probably right because i increased the size (the "n") from 10000 to 50000 and the number retained the size and the initial and final digits were the same. This is the source code i used, i used a VERY BIG number for the integer size just to test, i didn't actually bothered to see on what lenght the program would start do do stuff wrong but i know that the lenght of up to the 10.000th fibonacci number won't surpass the lenght that i defined because before this i made the program 'cout' every result until it got to 10.000th and it was always growing. I also checked the first numbers of the sequence before when i paused the program and i was seeing the 'digits grow' and confirmed the first fibonacci numbers of the sequence and they were correct. NOTE: This source code will only display the number of the fibonacci sequence that you want to know, it will only show you the numbers "growing" if you uncomment the commented lines.
#define TTMATH_DONT_USE_WCHAR
#include <ttmath/ttmath.h>
#include <iostream>
using namespace std;
int main () {
int fibonaccinumber;
cin >> fibonaccinumber;
cin.ignore();
ttmath::UInt<10000> fibonacci1,fibonacci2,fibonacci3;
fibonacci1 = 1;
fibonacci2 = 1;
//cout << "1. " << fibonacci1 << "
2. " << fibonacci2 << "
";
for(int i=3;i<=fibonaccinumber;i++)
{fibonacci3 = fibonacci1 + fibonacci2;
// cout << i << ". " << fibonacci3 << "
";
fibonacci1=fibonacci2;
fibonacci2=fibonacci3;}
cout << "the " << fibonaccinumber << "th fibonacci number is " << fibonacci2;
string endprog;
getline(cin,endprog);
return 0;}
我还没有修改这个图书馆的任意精度浮点数,但是当我这样做时,如果我看到人们对此指南感兴趣,我将继续扩展本指南,感谢所有评论和回答.
I didn't tinkered with arbitrary precision floats of this lbrary yet but when i do i will continue to expand this guide if i see that people are interested in it, thanks for all the comments and answers.
推荐答案
官网(http://www.ttmath.org/)有使用整数 (ttmath::Int<2> a,b,c;
) 和浮点数 (ttmath::Big<1,2> a,b,c;
) 两者.只需将它们视为没有成员的高精度 int
/float
,一切都应该没问题.如果错误仍然存在,您能否发布完整的错误消息以及错误所在的代码行?
The official site (http://www.ttmath.org/) has samples of using integers (ttmath::Int<2> a,b,c;
) and floating points (ttmath::Big<1,2> a,b,c;
) both. Just treat these like high precision int
/float
without members and everything should be fine. If the error remains, can you post the full error message, and the lines of code that it errored on?
这篇关于在 C++ 中处理任意长度的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中处理任意长度的整数
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01