How can C store a 1001-bit number in a double data type?

This is the code for calculating the 1000th power of 2.

#include <stdio.h> int main() { double multiply = 1; int i; for(i = 1; i <= 1000; i++) { multiply *= 2; } printf("%lf\n", multiply); return 0; } 

And the output on my system as well as ideone

 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 

which is the correct answer:

 irb(main):001:0> 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 == 2 ** 1000 => true 
+4
source share
1 answer

According to IEEE 754 , floats, etc. stored in a format with two capacities: sign, mantissa and indicator for base 2.

So, 2 ^ 1000, they just say, it is stored with the mantissa exactly 1 and an indicator of 1000.

If you add 2, this value will be invalid.

+15
source

All Articles