small factorial using double

Why can’t we use a double variable to store factorial of a number?

It’s because of range. The range of double is +/- 1.7e +/- 308 (~15 digits). Do you think factorial of large numbers will be of 15 digits?? Certainly not!!

To elaborate more on this, take the case how floating point numbers are stored in memory. Since floating point numbers are stored having 1 sign bit, 8 bits for exponent and 24 precision bits. Thus, the precision in decimal is log10(2^24) ≈ 7.225 i.e. 7 decimal digits.

Similarly working out for double precision numbers, there are 53 significant digits, i.e. in decimal is log10(2^53) which is nearly 15. Thus double can have the numbers with maximum precision of 15 digits.

The double-precision floating point numbers are stored in memory with 1 sign bit, 11 bits for exponent and 52 for the mantissa. Refer here for visual representation.

Due to floating point arithmetic, the exact value won’t fit, but you can get some of the first digits (around 15 of them).

Refer this link:

Largest number that can be stored in double is 9007199254740992L i.e. 2^53.

Double variable is 15 digits precision and so you may assume largest number to be 9*10^15.

Also for verification above SO link has a code for testing largest number you may check it… :slight_smile:

In small factorial, the range of input is given between 1 and 100 and this is what 100! is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
A double variable cannot hold a value of upto 150+ decimal places. Thats why we cant use it.
For further details visit: link

But 1.7e308 means 1.7*10^308 and that would mean around 308 digits.

This was helpful .Thanx:)

@sachinsahoo11 Thanks and Always remember marking the answer as accepted after it satisfies you, this helps others knowing the best possible answer.

1 Like