views:

2483

answers:

8

I am doing 2^1000 and am getting this:

1.07151e+301

Is there any way to actually turn this into a proper number without the e+301 or at least can anyone show me where I can see how to turn this in to a real number, by some way working with the e+301 part

Thanks

+8  A: 

You need to use a number class specifically designed for long numbers.

To represent 2^1000 as an exact number then by definition you need a number format that actually holds 1001 binary bits. The longest normal primitive integer format is usually only 64 bits.

BTW, the answer is:

% perl -Mbigint -e 'print 2**1000'
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
Alnitak
fixed to correct the off-by-one error
Alnitak
If you try to take the perl from the Mbigint, the Phantom will beat you up and give it back to them. It's not nice to rob pygmy tribes. Unless you're Indiana Jones.
Peter Wone
I assume you know this, but this is a Project Euler question. Some might consider it a bit distateful to explicitly spell out most of the solution.
Beska
nope, didn't know that at the time.
Alnitak
A: 

You are getting as precise a number as the variable type can support. That number is on the order of 1 followed by 301 zeroes. To get a precise number you'll have to work with a library that supports large numbers, or work with a language that is made for that kind of math (maple, matlab, etc)

tloach
+12  A: 

There is a practical limit to how large a number that can be directly manipulated in machine registers can be. if you are using double precision floats there are a total of 64 bits, some of which are devoted to the mantissa, some to the exponent, and 1 to the sign bit.

2^1000 needs a 1001 bit integer to be represented without losing precision. In order to work with numbers like that you will need to use a library that has big number support, such as GNU MP.

Louis Gerbarg
1001 bits, actually :) 2^1 needs 2 bits; and by induction from there.
Jonathan Leffler
You are correct, thanks for pointing it out, I edited to reflect that.
Louis Gerbarg
The ending seems to be cut off from the first sentence:"There is a fundamental limit to how large of a number can be" should actually say "There is a _practical_ limit on how large a number can be represented in a machine register."
florin
Clarified, thanks florin
Louis Gerbarg
A: 

Normally in C# we do:

Double myVal;
Double.TryParse("1.07151e+301", out myVal);

Now, are you in C++ .NET or in regular C++?

Maxim
@Maxime: I think you misunderstood the question.
andreas buykx
+1  A: 

cout << fixed << your_number;

But it won't probably show the whole number. As someone said before, you need to write a class.

+2  A: 

If you want to do it yourself in C++, you can for example create an digit array and do the calculation yourself. Tested and verified example:

unsigned int result[400]; // result digits
unsigned int i, j, carry;

// Initialize result digits
for (i = 0; i < 399; i++) {
  result[i] = 0;
}
result[399] = 2;

for (i = 2; i <= 1000; i++) { // Calculate 2^i
  carry = 0;
  for (j = 399; j > 0; j--) {
    result[j] <<= 1;    // multiply with 2
    result[j] += carry; // add carry
    carry = result[j] / 10;
    result[j] %= 10;    // we want one digit (0-9) only
  }
}

printf("2 ^ 1000 = ");
// print result digits
for (i = 0; i < 400; i++) {
  if (result[i] != 0) { // no leading zeros, please
    for (j = i; j < 400; j++) {
      printf("%d", result[j]);
    }
    break;
  }
}
printf("\n");
schnaader
+3  A: 

So, I'm thinking that what you really want is just the ability to print it without scientific notation. If you're using printf, what you want is:

printf( "%f1000.0", value );
// note that 1000 is way larger than need be,
// I'm just too lazy to count the digits

With cout, try something like:

cout.setf(ios::fixed);
cout << setprecision(0) << value;

If you want to print it as a power of two (2^1000 vs 10715...), you're on your own.

tvanfosson
Thanks this really showed the number correctly :)
AntonioCS
Is there any way I can place the output in a string instead of printing it on the screen??
AntonioCS
Just use snprintf. Note that since this number is just a power of two, you don't lose any information, but if you we to try the same thing with say 3^1000, a double wouldn't be able to store all the needed precision.
Eclipse
Or if you are using the C++ stream technique, use a std::ostringstream instance instead of cout.
Fred Larson
Thanks Josh and Fred :)
AntonioCS
+1  A: 

One option, if your application logic will allow it is to change the units you are manipulating....

If you are measuring the distance from New York to Paris in Angstroms, choose Miles or Kilometers instead.... Except for pure mathematical requirements, (like say factoring prime numbers for cryptology or, ... research into the Reimann Hypothesis), there is seldom any need to retain that many digits of accuracy.

On the other hand, if you are doing something that requires perfectly accurate integer values with that many digits, then you should probably get specialized software designed to handle large numbers... Such software is definitely available, although I'm not familiar with that area. (costs, vendors, capabilities etc.) If cost is an issue, and you're thinking of writing your own, I don't know enough about what's involved in to know if that approach is worth the effort...

Charles Bretana