Thread: Long Integers

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Long Integers

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    It is too large for both unsigned and long long.

    About to try GMP

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    Right, after playing around with GMP I still can't get it to work. Any good resources on it other than their documentation?

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    How long the number is. How many digits does the number contain?

    ssharish2005

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    I assume big doesn't cut it?

    In one case 109 digits in base 10

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    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 ).

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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right, after playing around with GMP I still can't get it to work. Any good resources on it other than their documentation?
    As in you cannot get it to compile without errors? Personally, I found the documentation to be pretty good.

    I assume big doesn't cut it?
    The GMP Manual states:
    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.

    In one case 109 digits in base 10
    109 digits in base 10 is about 360 bits, so GMP should not have a problem handling that.

    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 ).
    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 Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think that my problem is windows!
    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 Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    I tried dealing with it using MSYS, but I didn't find out where to put the archive...

    Are there any precompiled versions anywhere?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I tried dealing with it using MSYS, but I didn't find out where to put the archive...
    It is quite simple, actually:
    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.

    Are there any precompiled versions anywhere?
    Probably, but then they would be unofficial.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    @laserlight - I meant where on my computer do I put the archive (where does MSYS store stuff)

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    @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 Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. long int type
    By sarahr202 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2009, 12:55 PM
  2. Long Integers
    By kabuatama in forum C Programming
    Replies: 24
    Last Post: 01-28-2006, 01:21 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  4. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM