How to define bigger integers?

Nov 30, 2008 at 9:06am
As is said above. I want to make a simple calculator and even long long looks to short for my needs. How do I define bigger integer?
Thanks in advance.
Last edited on Nov 30, 2008 at 9:08am
Nov 30, 2008 at 9:26am
How precize should they be? If the numbers dont need to be very precize, you could use a class with two members: a double for the value and a (long long) integer for the e-power (eg 5.24135 * 10^133).
You can also use 'pen and paper methods'. This means you calculate the answers number by number. For examplel, consider the following recursive function to divide numbers very precizely:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int size=1000;
int count=0;

//userinput:
int numerator;     
int denominator;

void getAnswer (int count)
{     
     cout<<numerator/denominator;

     if ( count<size)
     {
        if (count==0)
        cout<<".";
        numerator%=denominator;
        numerator*=10;
        count++;
        getAnswer(count);
     }
}
Last edited on Nov 30, 2008 at 9:27am
Nov 30, 2008 at 9:51am
If you need full precision and arbitrarily long numbers, you should google "arbitrary precision arithmetic". I'd say the most popular one is the GMP (GNU MP Bignum Library). The only constraint in size is available memory.

Other than bignums, the largest datatype in C++ is the long double. In 32-bit systems, a 96-bit floating precision value. Pocket calculators use double, so I doubt you'll need a larger number than that. Unless, of course, you want to compute 2^(256^2), which I think it's totally acceptable.
Nov 30, 2008 at 9:17pm
Not necessarily. The only requirement is that sizeof( long double ) >= sizeof( double ). For example, in MSVC++ a long double is 64 bits -- just like a regular double.

I believe that the GCC will give you the 80-bit IEEE double extended precision if you ask for it (via options).

+1 for GMP

Hope this helps.
Nov 30, 2008 at 9:41pm
You're right about VC++.
However, MinGW did give me the size of long double as 96 bits. With no additional options.
Dec 1, 2008 at 12:05am
Slick! Must be something to the latest version... (I hadn't checked since 3.x)

:-)
Topic archived. No new replies allowed.