Reputation: 8496
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?
Upvotes: 27
Views: 39510
Reputation: 21
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
Upvotes: 0
Reputation: 11
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.
Upvotes: 0
Reputation: 49719
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");
Upvotes: 4
Reputation: 43452
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.
Upvotes: 13
Reputation: 146499
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...
Upvotes: 2
Reputation: 532435
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.
Upvotes: 16
Reputation: 339816
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
Upvotes: 10
Reputation:
cout << fixed << your_number;
But it won't probably show the whole number. As someone said before, you need to write a class.
Upvotes: 2
Reputation: 8040
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)
Upvotes: 0