Variable's maximum values

Hey guys, I've got a question about variable maximum values, not just in C++, but in programming in general.

Days ago I programed the Karatsuba algorithm, which is used for integer multiplication with big numbers, since it's faster than the traditional multiplication method. I've had problems testing it's efficiency because of the max values of long long int.

My question is: when huge calculations are done (I'm think about cryptography, NASA, university investigations, financial markets, etc). How do they solve the problem of max values? Do they use different languages that are capable of handling them? They somehow avoid the limitations while using languages like C++?

Curiosity is killing me.

Thanks for your time!
Last edited on
I do not see any problem. You can use spacial libraries that realize arithmetic of large numbers.
Arbitrary precision arithmetic: basically storing numbers as dynamic arrays (well maybe that's an oversimplification).

http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic
http://stackoverflow.com/questions/1218149/arbitrary-precision-arithmetic-explanation

For C++ you can use a number of third party libraries.
http://gmplib.org/

Other languages, such as C#, Java and D have out-of-the-box BigInteger libraries, or even better, have built-in support (for example J and maybe Haskell).
> Do they use different languages that are capable of handling them?
> They somehow avoid the limitations while using languages like C++?

The basic limitation arises from the processor, not the programming language. Mathematical operations on numbers larger than what the processor instructions can support is performed using algorithms implemented in software.

These algorithms may be available as built-ins, may be part of the standard library, may be available in a third-party library, or may be just a set of functions in the program.

In C++, we typically use libraries; for instance GMP is popular. There is a proposal for an arbitrary precision integer arithmetic library to be included in C++14.
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3542.html

Won't be an earth-shattering development if it is either accepted or rejected; though acceptance could make life a lot easier for people starting out to learn C++.
If you're looking for a challenge, you could try and write your own arithmatical operators, using an array or STL container. ;) You would, then, have truely unlimited numbers.
Thanks to all for your answers. I haven't still faced a problem which needed anything of this to be solved, but I'll keep your explanaitions and links just in case.

And IWishIKnew, looks challenging right know, maybe someday hahaha.
And IWishIKnew, looks challenging right know, maybe someday hahaha.

Personally, I would advise against doing it. It's complicated and a waste of time.

However, if you want to do it anyway, instead of using libraries such as GNU GMP or waiting for possibly a standard library in C++14 or C++17, we'll be here to help. And here's a possible start:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class BigInteger
{
private:

    std::vector<unsigned char> hex_digits;
    bool positive;

public:

    bool operator == (const BigInteger &bi) const
    {
    }

    bool operator < (int i) const
    {
    }

    // ...
};

BigInteger operator + (const BigInteger &lhs, const BigInteger &rhs)
{
}

BigInteger operator + (const BigInteger &lhs, int rhs)
{
}

BigInteger operator + (int lhs, const BigInteger &rhs)
{
}
Last edited on
Topic archived. No new replies allowed.