Handle arbitrary length integers in C++ [duplicate]
Asked Answered
B

3

14

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.

Bigeye answered 16/11, 2011 at 4:56 Comment(22)
"Precision float" seems like an oxymoron to me.Leffen
Use mpir. Also there is no "quick fix" for this, arbitrary precision arithmetic done well is a big thing to ask.Trysail
gmplib.org is a well maintained library.Pumpernickel
@Andrew Marshall how is that supposed to help? also floats have a defined degree of maximum precision or 'significant digits', that's just the name/designation it has, i also find it incorrect but whatever...Bigeye
I second gmplib.org. Two headers(one for c, one for c++), two libraries(one for c, one for c++) and you are done. It is not the fastest library out there, but it is simple to use.Sassafras
@parapura rajkumar i saw that before, couldn't understand a thing on how to set it up and use it, if you do please answer belowBigeye
@Bigeye Which ide, and which os are you using?Sassafras
Maybe you should remove the rant parts of this question. This question might get more traction if you list all the libraries you've tried, and what didn't meet your requirements in each case, then list the things you need out of your library. If you've said this in your question, I lost it under your ranting :)Gandzha
@Seth Carnegie that one seems to have some potential, it has more documentation than the rest at leastBigeye
@Ethan Steinberg windows 7 on an x64 system, i use codeblocks as an ide and the latest mingw compilerBigeye
@Merlyn Morgan-Graham updated the question with more info. just ignore the rant part and head straight to the end pleaseBigeye
@Bigeye just so you know, mpir is like a windows port of gmp with improvements. I would be surprised if it wasn't good for you.Trysail
If you’ve solved your problem, you should post your answer below (which you can accept after 48 hours). You should not simply edit your question with the solution.Leffen
@Andrew Marshall I could post my answer and accept it below but the answer I chose pointed me in the right direction to solve my problem and so it deserves credit for that (also my answer wouldn't help me to find an answer to my problem, obviously). I have the right to edit my question (in this case adding information after it) to tell how things were solved and to help others who will face the same hardships as I did, in part that's what stack exchange is all about, helping others, if you don't like it there's nothing I can do for you, just don't mess up the stuff from those who do.Bigeye
@Bigeye If it’s an answer, it doesn’t go in the question. Period. You calling it an answer and “how things were solved” makes it evident it doesn’t belong in the question. Nothing says you can’t give credit to other answerers or that you have to accept yours. See also Should own solution be included within the question? on Meta (answer by SO mod). Note that answering your own question is officially encouraged.Leffen
@Andrew Marshall 1st-I haven't called it an answer. 2nd-What I've added to what you call "the question" wasn't an answer, look up "answer" in the dictionary if you think it is. 3rd-It's an update, that's why it's written "update" there, if you disagree, check the dictionary again but this time for "update". 4th-Even if it was in the "wrong place" that wouldn't justify deleting it because as it was before it would help people, as you left it, it wouldn't help people, it would be of much better tone and consideration of you to ask me to delete that content and post an answer with it instead...Bigeye
@Andrew Marshall ...of just deleting it which is pretty much the same thing as pissing in the work I had to compile that information and the work other members had in making my text formatted and corrected and negating that information to possible viewers with problems just because you think "that shouldn't be there, it should be elsewhere". You "think" being the key word here, because up until now your arguments for deleting that information were moot.Bigeye
@Bigeye 1. You above: “I could post my answer…” & “edit my question…to tell how things were solved”. 2. As I said, you called it an answer and solution. 3. Update is non-specific. 4. It’s not my fault you weren’t here to post it as an answer, I commented to notify you—would you rather me have posted it in your absence and took credit for it? Not I think—see the meta post I referenced where an SO mod echoes what I’m saying.Leffen
@Andrew Marshal No, I didn't, you're either misunderstanding or deliberately quote mining, put things in context and see what you've said before that in turn generated that response from me. Update is specific, I've specified the nature of the update on the text and have you seen the dictionary yet? Do so. You commented to notify me? Stop lying, a lot of people can see the dates of the comments and the dates of the edits, you edited it first removing a chunk of it and commented later.Bigeye
@Bigeye “I have the right to edit my question (in this case adding information after it) to tell how things were solved”. If you’re adding how things were solved then it’s a solution, which goes in an answer. I’m not quote mining or taking out context. Both sentences in that comment you refer to your changes as either “my answer” or “how things were solved”. Nothing says otherwise there.Leffen
@Andrew Marshal Last comment. Let's say for a second that you're absolutely right and that you really acted on good faith by removing an answer from a question and all that. Now, why should I even bother with what you're saying anyway? I mean, I do not need to alter my "question", there's no rule that I'm infringing, it was all fine for everybody (800 views 10 positive ratings) until you came along. I'm fairly tolerant, and I would change it if you were acting on good faith, like, for example, telling me to post as an answer and take the information out of there, but you're not.Bigeye
@wxiir My good faith is not up to you to decide, as it’s about my own intentions, which you (of course) cannot certainly dispute. And my intentions were (and are) nothing but good.Leffen
S
4

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?

Sambo answered 16/11, 2011 at 18:25 Comment(4)
That's why i used "typedef ttmath::UInt<100> BigInt;" to be able to use the BigInt type like the guy said on the post, so the problem is not that. Also the compiler "accepted" the line "typedef ttmath::UInt<100> BigInt;" and only freaked out when i declared a variable as BigInt type. it compiled an example program with floats, maybe the guy on the post i mentioned is not being very specific about what to do later and it's in fact more complicated than he makes it look like or maybe he's forgotten something important there. please next time dont remove the comment, this was my answer to itBigeye
Then you'll have to post the full error message and the lines of code that error'd or we'll be unable to help you.Sambo
I think i finally got it. I will see what i can do with this and when i can i will update my answer with my findings, your answer definitely helped me because after re-reading it i understanded that maybe you were trying to say to me to don't try to build a type out of ttmath and keep it simple until it works. i also had seen the samples they had before but didn't understanded a thing but now i think i do. P.S. i still get errors following what the other guy says but it seems to work like this (declaring variables using that long ttmath code) so i will keep it this way.Bigeye
this ttmath library works like a charm, i calculated up to the 100000th fibonacci number that was like 3 pages long in like 15 or 20 seconds on just one core, i will update my postBigeye
D
1

The Boost.Multiprecision library supports arbitrarily long integers, real numbers and ratios. It also allows you to use different back-ends which have different performance characteristics and licensing terms.

Dharana answered 14/6, 2014 at 14:55 Comment(0)
R
0

A few possibilities are MIRACL, NTL and LIP.

Redan answered 16/11, 2011 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.