Rezwan Ibne Borhan
Rezwan Ibne Borhan

Reputation: 43

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.......

Upvotes: 1

Views: 3795

Answers (4)

MagnusEffect
MagnusEffect

Reputation: 3925

Try this to get the answer rounded off upto 100 decimal points.

    double long a,b,n;
    cin>>a>>b;
    n=a/b;
    std::cout << std::fixed;
    std::cout << std::setprecision(100);
    std::cout << n << '\n';

Upvotes: 0

chux
chux

Reputation: 154582

The printing is not the problem. Code is well printing the quotient as a double.

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

OP's reported result
3.142857142857142793701541449991054832935333251953125.......
// My expected result of the double 22.0/7.0
3.1428571428571427937015414499910548329353332519531250000000....

Using double math, in general, will not provide OP with 100 digit accuracy. Here is why.

Different explanation approach

A double is typically 64-bits. That gives up to 264 combinations of exactly representable numbers. Consider double n,d,q; and dividing n/d. That is 2128 different combinations of n,d yet we are trying to put all those results in q. Many will result in the same answer (22.0/11.0 and 14.0/7.0 both make 2.0). Yet there are certainly more than 264 different mathematical answers.

So C rounds the answer to the best - typically closest - representable double.

In this case: 22.0/7.0, exactly, in not one of those 264 combinations.
The "best" is "something like 3.142857 142857 142793 7015414..."

double x = 22.0/7.0;
//  x   3.142857 142857 142793 701...

// 22.0/7.0
// math 3.142857 142857 142857 142857 

double next_x = nextafter(22.0/7.0, 4.0);
//      3.142857 142857 143237 790...

Upvotes: 0

Pavan Chandaka
Pavan Chandaka

Reputation: 12831

When I try

int main()
{
   const long double pi = 22.0/7.0;
   std::cout << std::setprecision(std::numeric_limits<long double>::max()) << pi << '\n';
}

I am getting segmentation leak.

The max you get is:

int main()
{
    const long double pi = 22.0/7.0;
    std::cout << std::setprecision(std::numeric_limits<long double>::digits) <<   pi << '\n';
}

output is: 3.142857142857142793701541449991054832935333251953125

Upvotes: 0

yd1
yd1

Reputation: 277

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

Upvotes: 1

Related Questions