Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid scientific notation for large numbers?

Tags:

c++

math

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?

like image 989
AntonioCS Avatar asked Nov 05 '08 14:11

AntonioCS


People also ask

How do you stop large numbers from scientific notation in Excel?

Unfortunately excel does not allow you to turn this functionality off by default. However if you select your data, right click, and click "Format cells..." and choose Number you can stop excel from changing your data to scientific notation.

How do you express very large numbers in scientific notation?

Scientific notation is a way of writing very large or very small numbers. A number is written in scientific notation when a number between 1 and 10 is multiplied by a power of 10. For example, 650,000,000 can be written in scientific notation as 6.5 ✕ 10^8.

How do you not use scientific notation?

If you want to avoid scientific notation for a given number or a series of numbers, you can use the format() function by passing scientific = FALSE as an argument.

How do I get rid of +11 in Excel?

you can right click on the cell and choose Format cell and change the format from general to number with zero number of decimal places.


3 Answers

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.

like image 199
tvanfosson Avatar answered Oct 14 '22 15:10

tvanfosson


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.

like image 24
Louis Gerbarg Avatar answered Oct 14 '22 17:10

Louis Gerbarg


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
like image 39
Alnitak Avatar answered Oct 14 '22 16:10

Alnitak