27

I am doing 2^1000 and I 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?

Pang
  • 9,564
  • 146
  • 81
  • 122
AntonioCS
  • 8,335
  • 18
  • 63
  • 92

9 Answers9

15

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
  • 524,688
  • 99
  • 697
  • 795
  • Is there any way I can place the output in a string instead of printing it on the screen?? – AntonioCS Nov 05 '08 at 19:34
  • 1
    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 Nov 05 '08 at 19:50
  • Or if you are using the C++ stream technique, use a std::ostringstream instance instead of cout. – Fred Larson Nov 05 '08 at 21:09
  • @tvanfosson I also have similar question [here](http://stackoverflow.com/questions/35258164/how-to-prevent-numbers-from-showing-up-in-scientific-notations). If possible can you help me out? – user1950349 Feb 14 '16 at 19:34
12

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
  • 43,356
  • 8
  • 80
  • 90
  • 1
    1001 bits, actually :) 2^1 needs 2 bits; and by induction from there. – Jonathan Leffler Nov 05 '08 at 14:40
  • You are correct, thanks for pointing it out, I edited to reflect that. – Louis Gerbarg Nov 05 '08 at 14:43
  • 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 Nov 05 '08 at 14:46
10

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
  • 334,560
  • 70
  • 407
  • 495
  • 1
    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 Nov 05 '08 at 14:45
  • 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 Mar 10 '10 at 14:15
4

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
  • 49,103
  • 10
  • 104
  • 136
2

cout << fixed << your_number;

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

2

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
  • 143,358
  • 22
  • 150
  • 216
0

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
  • 8,009
  • 1
  • 33
  • 44
0

Include the header limits.h and cmath.h

cout.precision(0);
cout<< fixed<< pow(2,31);               //OR ANY NUMBER HERE

Use cout.precision to set the precision.

TobiSH
  • 2,833
  • 3
  • 23
  • 33
0

I was facing the same issue. I was trying to directly print 2^34 and the output was 3.43597e+010.

You should consider storing it in a variable, as this solved my problem. For example,

long long int a= pow(2,34)-2;
cout<<a;

And the output was a real number, just as I wanted - 34359738366

Raj Aryan
  • 21
  • 1
  • 7