Hello,
I did search for this, but as you can imagine, searching for long integers on a C forum is not the easiest of tasks!
I was wondering if anyone could help me deal with 'long' integers which are too large for even the long type?
Thanks
Simon
Printable View
Hello,
I did search for this, but as you can imagine, searching for long integers on a C forum is not the easiest of tasks!
I was wondering if anyone could help me deal with 'long' integers which are too large for even the long type?
Thanks
Simon
In C99 at least, you could try using long long. If not, your best bet would be a bignum library like GMP. Of course, if the values that you are trying to store are not much bigger (read: less than twice as large) than the maximum long value, and you do not need negative values, then perhaps unsigned long is what you should use.
It is too large for both unsigned and long long.
About to try GMP
Right, after playing around with GMP I still can't get it to work. Any good resources on it other than their documentation?
How long the number is. How many digits does the number contain?
ssharish2005
I assume big doesn't cut it?
In one case 109 digits in base 10
What kind of operation are you going to do on those long integer ? If it's only simple arithmetic (+, -, *, /, sqrt, exp) i guess a good amout of people on this forum did some kind of BigNum library (like me :D).
Fairly easy to use. But far from being optimized (it's fine for something else than operation intensive use, like factoring large semi prime numbers). And it's somehow complete for "basic" use.
Let me know if you are interested heh...
As in you cannot get it to compile without errors? Personally, I found the documentation to be pretty good.Quote:
Right, after playing around with GMP I still can't get it to work. Any good resources on it other than their documentation?
The GMP Manual states:Quote:
I assume big doesn't cut it?
Many applications use just a few hundred bits of precision; but some applications may need thousands or even millions of bits. GMP is designed to give good performance for both, by choosing algorithms based on the sizes of the operands, and by carefully keeping the overhead at a minimum.
109 digits in base 10 is about 360 bits, so GMP should not have a problem handling that.Quote:
In one case 109 digits in base 10
Chances are your implementation of a bignum library pales in comparison to GMP. If GMP cannot handle it, then your "far from being optimized" library probably cannot either.Quote:
If it's only simple arithmetic (+, -, *, /, sqrt, exp) i guess a good amout of people on this forum did some kind of BigNum library (like me ).
I think that my problem is windows! (No change there then)
I'm at someone else's house and therefore most obvious solutions cannot be achieved, for instance, cygwin, dual boot or live cds.
I have search google for GMP on windows most sites recommend cygwin.
I have been able to compile GMP 4.2.1 on MS Windows XP SP2 with the MinGW port of GCC under MSYS with no errors (including make check). I had some success with MSVC8 in the past (I think I built GMP with MinGW but was able to share the results with MSVC8), but I have not tried recently.Quote:
I think that my problem is windows!
I tried dealing with it using MSYS, but I didn't find out where to put the archive...
Are there any precompiled versions anywhere?
It is quite simple, actually:Quote:
I tried dealing with it using MSYS, but I didn't find out where to put the archive...
1. Download gmp-4.2.1.tar.bz2 or gmp-4.2.1.tar.gz
2. Extract to say, a gmp-4.2.1 directory
3. Go to that directory using MSYS
4. Enter ./configure --prefix=/mingw
5. Enter make
6. Optional: Enter make check
7. Enter make install
If you want to enable the C++ interface, then substitute step #4 with:
./configure --prefix=/mingw --enable-cxx
If you want a shared library (e.g., for use with Microsoft's compilers), then it should be:
./configure --disable-static --enable-shared
Read Notes for Particular Systems on how to get it to work with MSVC. You could try compiling with MSVC directly, but so far the MinGW+MSYS route has worked for me.
Probably, but then they would be unofficial.Quote:
Are there any precompiled versions anywhere?
You might wish to try the bignum class which I found on the net a long time ago and brought it up to scratch with modern C++, fixing bugs etc.
You can find it on my useful classes page from the link in my sig.
@laserlight - I meant where on my computer do I put the archive (where does MSYS store stuff)
Any normal directory. MSYS is only an environment from which you can run configure scripts (and other related utilities).Quote:
@laserlight - I meant where on my computer do I put the archive (where does MSYS store stuff)
For some reason it keeps putting me in the same directory tree which I cannot escape out of
What do you mean? You can use cd to change directory (just like on Windows, but with the option of *nix-like paths).Quote:
For some reason it keeps putting me in the same directory tree which I cannot escape out of
Yeah, I found out how now... I was just doing cd .. to get as high as I could go, but it turns out what I really needed was cd "C:\path\to\dir"
Followed all the steps, now can I just #include <gmp.h> or what?
Yes, but read up on GMP Basics.
How do I "include the library" using a single file in Bloodshed?
You would need to transfer include/gmp.h from the MinGW directory to Dev-C++'s directory. Do likewise for lib/libgmp.a
After that it is a matter of setting project options. Probably something like -lgmp under linker options.
I got it working for projects, I wanted to know if I could do it for single files.
No idea, sorry. I would probably use gcc directly if I wanted to work with single files.
Thanks for your help
>I got it working for projects, I wanted to know if I could do it for single files.
Go to: Tools -> Compiler Options, then check: Add these commands to the linker command line, and then add -lgmp as Lasarlight mentioned.