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 s
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
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.
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");
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...
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.
cout << fixed << your_number;
But it won't probably show the whole number. As someone said before, you need to write a class.