Search code examples
c++floating-pointdoublefloating-accuracy

How to print upto 100th decimal places accurately of a float or double type variable?


Suppose there are two numbers 22 and 7. I want to print up to the 100th decimal place of the answer which should look like 3.1428571428571428571428571.... whereas trying to use

cout << setprecision(100) << fixed << (22.0/7.0); 

I get something like 3.142857142857142793701541449991054832935333251953125.......


Solution

  • generally in c++, the double (the bigger of the two) is stored in 8 bytes. that's means that the number can contain 2^8*8 = 2^64 byes (^ = power). that's means the biggest number of digits after the point, after keeping the sign and the floating point location, would be at most ~15 digits. therefore you will have to create other means then double for this calculation. here is my reference: https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm