20

I've tried using

long long int

But it wont work for numbers like 3141592653589793238462643383279502884197169399375, I need this up to 10 ^ 80. Any idea? Let me know. Thanks alot.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user1815324
  • 269
  • 1
  • 4
  • 10
  • You need to use either string or character vectors to represent big integers like this. – taocp Mar 14 '13 at 02:24
  • 8
    Instead of creating your own, you could consider using GMP (http://gmplib.org/) – FatalError Mar 14 '13 at 02:26
  • The BN library out of OpenSSL is also decent for basic big-int stuff, though it may not have *every* numeric feature you are looking for. Its homed around crypto, after all. – WhozCraig Mar 14 '13 at 02:28

1 Answers1

31

You can't use any built-in integer type for this. You need a "multiple precision integer" aka "bignum" library. For C++, I would try Boost.Multiprecision first, but be aware that Boost can be considerably more trouble than it is worth, particularly if the module you're using has any shared library (aka DLL) component. The other obvious choice is GNU MP. It only has a C interface, but it is well-maintained, reliable, fast, and very popular (in fact, it appears that Boost.MP is "just" a C++ wrapper for it!)

WARNING: You might want a bignum library because you are trying to implement one of the cryptographic primitives that uses huge numbers, like RSA. Do not do this. The generic bignum libraries are not safe for cryptographic use, and even if they were, there would still be dozens of subtle mistakes you can make that would ruin your security. Use a well-tested cryptography library instead; for C++ I recommend Botan.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 3
    The problem is: I don't want to use any library, is it possible? – user1815324 Mar 14 '13 at 02:33
  • 24
    Like many things in life, while possible it is not advisable. – Ben Mar 14 '13 at 02:37
  • It is not that I want to do this the hard way, it's because my assignment says so. – user1815324 Mar 14 '13 at 02:38
  • @user1815324, Then it would be a good idea to get started on making your own by reading some design ideas for such a class (should digits be stored in a string? An array of integers?) and picking one you like best and tweaking it however you want. Once you have the underlying design figured out, start filling in the necessary functions and if you can't find an answer to a problem you have in one of the many bignum questions floating around, a specific question about how to properly implement one of those functions would fit well here. – chris Mar 14 '13 at 02:49
  • 6
    -1 for scaring people away from Boost because "it's more trouble than it is worth, particularly if the module you're using has any shared library component". Dependency on a Boost library that's built as a DLL is no different from dependency on any other library that's built as a DLL. Static linking is also available, and solves most of the headaches with dynamic linking (not just for Boost, but in general). – HighCommander4 Mar 14 '13 at 07:30
  • 1
    @HighCommander4 You clearly have not had to maintain a piece of software that links against Boost DLLs. It is a never-ending parade of unflagged ABI breakage. I suppose it may have gotten better since I stopped working on that project, but from 2006 to 2010 there seemed to be no interest from the Boost side in making things better, so I doubt it. Furthermore, even if you stick to header-only, just getting the damn headers installed in your build environment may well be more trouble than it's worth IMNSHO. It's not the least-cooperative setup process I've ever seen but it's probably number two. – zwol Mar 14 '13 at 12:28
  • 1
    @Zack: ABI breakage? When you download a new version of the headers, you rebuild the libraries. How often do you download a new version of the headers - surely not often enough that it's too much of a pain to rebuild the libraries. Getting the headers installed? You add *one* -I flag to your compiler command line (or corresponding flag/line/whatever to your build system). Done. – HighCommander4 Mar 14 '13 at 20:29
  • @HighCommander4 Are you talking about some other project that happens to have the same name for some reason? "Download a new version of the headers", you say, as if it were a trivial thing rather than a multi-hour exercise in computational demonology. Iä! Iä bjam! *hollow, bitter laughter* – zwol Mar 14 '13 at 20:35
  • 1
    @Zack: Perhaps we are :) In my experience, downloading a new Boost version has been as simple as downloading the zip file from the website, extracting it, and running bjam to build it. I am curious what has made it an "exercise in computational demonology" in your experience. – HighCommander4 Mar 15 '13 at 05:59
  • @HighCommander4 This *was* some time ago and my memory of it is thankfully a bit fuzzy, but: if you don't get the configuration parameters *just right*, Boost will fail to build, fail to install, or (worst of all and yet somehow the most common outcome IME) appear to build and install fine, pass its selftests, but any program that uses any Boost DLL will crash inside the C++ runtime on startup. The defaults never worked for me, there was very little documentation of how to configure it, and often adjustments that were supposed to work didn't seem to have any effect at all. – zwol Mar 15 '13 at 15:35
  • 1
    @Zack: It *is* important that libraries be built in the same configuration as the applications they link to, I grant you that. But in my experience that's the case for C++ libraries in general, not just Boost. – HighCommander4 Mar 15 '13 at 17:36
  • @HighCommander4 I haven't had remotely as much trouble getting other C++ libraries to assume the same configuration as the applications that I wanted them for, as I have with Boost. For instance, the defaults pretty much Just Work with other C++ libraries. Not so Boost. – zwol Mar 15 '13 at 20:36