在C ++中处理任意长度的整数 [英] Handle arbitrary length integers in C++

查看:184
本文介绍了在C ++中处理任意长度的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我一个好的C ++库处理(做操作等...)与任意大的数字(它可以是一个库处理任意精度浮点,但处理整数更重要)。



请仅参考您使用的图书馆,告诉我您是如何设置和提取的,也许有一个非常简约的例子或东西上面提到的库缺乏良好的文档提供了自己的一些输入)。



对于记录我在x64机器上使用Windows 7,CodeBlocks作为我的IDE,最新的MinGW作为编译器。



我试过的库:




  • vlint


  • bigint(容易设置,编译错误和没有多少文档可能导致错误) p>


  • ttmath(似乎很有前途,编译了一些BIG示例程序,并且由于编译错误而无法理解语法, li>

  • gmp(甚至无法设置)




删除了'问题部分的问题',这基本上解释了为什么我问的问题在Stackoverflow的很多次,所以人们会读它到底。



- > UPDATE



所以我选择了一个不是直接回答我的初始问题,但帮助我很多解决这个,我会发布一些我的发现,帮助像我这样的其他c + +新手,开始使用非常大的数字,没有努力的图书馆的天,像我在一个简单的一步一步微导。



我使用的资料(请注意遵循指南):




  • Windows 7 Ultimate x64


  • Amd k10 x64


  • Code :: Blocks 10.05 不包含MinGW的版本,文件名为codeblocks-10.05-setup.exe(安装在C:\Program Files )\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上提取


  • TTMath 0.9.2 文件名称ttmath-0.9.2-src.tar.gz ttmath到文件夹C:\CPPLibs(这是我把我的c ++库放入的文件夹)




如何设置




  • 编译器和调试器(我的编译器在这里被自动检测。 如果这不会发生与您,在选定的编译器选择GNU GCC编译器,然后单击设置为默认,然后在工具链可执行文件在编译器安装目录,你可以选择安装编译器的目录或尝试自动检测,并在C ++编译器上排序的选择或写入mingw32-g ++。exe。 如果发生这种情况,只需执行此操作,在所选编译器上选择GNU GCC Compiler,然后单击设置为默认值)。


  • 无需离开代码:块>设置>编译器和调试器,并按上述整理,转到搜索目录,然后单击编译器 添加并选择存储库或放置ttmath文件夹(在我的情况下为C:\CPPLibs),然后转到链接器,并做同样的事情的文件夹。


  • 要使用ttmath库开始编码,必须将此行 #include< ttmath / ttmath.h& / code>之前的主要功能(注意:如果你使用64位系统,你会得到很多错误,如果你不把这行 #define TTMATH_DONT_USE_WCHAR 之前此行 #include< ttmath / ttmath.h> ,我一直在努力与这个垃圾,直到我发现修复,一些其他的人,也在努力找到并发布在网络上,它为我工作)ps我认为它只适用于64位系统,但如果你确实得到错误,只是因为包括ttmath.h头文件,很可能是因为这一点。


  • 声明变量必须具有大整数值。 ttmath :: UInt< ; n> a,b,c; 其中a,b,c是您的变量,n是可以以这种形式存储在变量中的数字的大小2 ^ )-1,对于64位系统,此形式为2 ^(64 * n)-1


  • / strong>如果你这样做 a = 333; (而不是333的数字大于c ++上的long int标准数据类型)编译,因为赋予这样的变量独立于你之前指定的整数的大小可以像c ++上的long int标准数据类型一样大(我想这个我自己的,硬的方式),甚至如果你使用一个更小的值,它编译好,然后运行你的程序,并试图写入这个变量一个更大的数字,所提到的long int标准数据类型可以处理的数字,那么你的数学将去错误所以请注意:向正确的方式赋值一个值,你必须像下面这样指定它: a =333; (是的,我知道你几乎把它作为一个字符串这种方式,但它会做的操作很好,没有任何问题,如果你决定cout变量,它永远不会是一个指数或科学记谱结果像你得到使用标准整数数据类型,而不与一些额外语句耦合以显示正好的数字)




ps使用这个简单的规则来处理整数和这个库我计算斐波那契数字到一个简单的程序(花了3分钟的代码)在15到20秒的一个简单的程序的100,000个数字,这个数字占用了3页,除了是一个实用图书馆,一旦你知道它是如何工作(你以前几乎没有帮助,一些ttmath网站的样本是相当误导,但现在你有一些帮助)它也似乎相当高效,我确认100.000的数字大概是正确的,因为我将大小(n)从10000增加到50000,数字保留了大小,初始和最后的数字是相同的。这是我使用的源代码,我使用一个非常大的数字的整数大小只是为了测试,我没有实际上困扰看看程序将开始做错误的东西,但我知道长度10.000的斐波纳契数不会超过我定义的长度,因为在这之前,我让程序cout每个结果,直到它达到10.000th,它总是增长。我还检查了序列的第一个数字之前,当我暂停程序,我看到'数字成长',并确认序列的第一个斐波纳契数字,他们是正确的。 注意:此源代码只显示您想要知道的斐波纳契序列的编号,如果您取消注释已注释的行,则只会显示增长的数字。

  #define TTMATH_DONT_USE_WCHAR 
#include< ttmath / ttmath.h>
#include< iostream>

using namespace std;
int main(){

int fibonaccinumber;
cin>>纤维阵列;
cin.ignore();

ttmath :: UInt< 10000>纤维蛋白1,fibonacci2,fibonacci3;
fibonacci1 = 1;
fibonacci2 = 1;
// cout<< 1.<纤维蛋白1 < \\\
2。<纤维二糖\\\
;

for(int i = 3; i <= fibonaccinumber; i ++)
{fibonacci3 = fibonacci1 + fibonacci2;
// cout<< i<< 。<纤维蛋白酶-3 \\\
;
fibonacci1 = fibonacci2;
fibonacci2 = fibonacci3;}

cout< the<<纤维阵挛< 纤维蛋白溶酶数是<纤维二细胞

string endprog;
getline(cin,endprog);
return 0;}



我没有修改任何精确浮动的这个lbrary但是如果我看到人们对它感兴趣,我会继续扩展本指南,感谢所有的评论和答复。

解决方案

官方网站(http://www.ttmath.org/)有示例使用整数( ttmath :: Int 2 a,b,c; )和浮点( ttmath :: Big& b,c; )两者。只是对待这些高精度 int / float 没有成员,一切都应该很好。如果错误仍然存​​在,您可以发布完整的错误消息,以及它错误的代码行吗?


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).

For the record I'm using Windows 7 on an x64 machine, CodeBlocks as my IDE, and the latest MinGW as the compiler.

Libraries I tried:

  • vlint (not enough operations, works fine for small stuff though)

  • bigint (easy to set it up, compile errors and not much documentation (from which errors might be derived))

  • 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 (couldn't even set it up)

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.

--> UPDATE

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 (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 the version without MinGW included, file name "codeblocks-10.05-setup.exe" (installed on C:\Program Files (x86)\CodeBlocks)

  • 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 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)

What to do to set it all up

  • 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").

  • 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.

  • 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.

  • 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

  • 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)

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 << "\n2. " << fibonacci2 << "\n";

for(int i=3;i<=fibonaccinumber;i++)
{fibonacci3 = fibonacci1 + fibonacci2;
//   cout << i << ". " << fibonacci3 << "\n";
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.

解决方案

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 ++中处理任意长度的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆