Show more precision in C ++

4

I have this program:

int main(){
    double x=2;
    cout << sqrt(x);
}

I would like to show the result as accurately as possible. Thanks!

    
asked by anonymous 22.12.2015 / 20:21

2 answers

5

Uses setprecision this way:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <limits>           // Para ter a máxima precisão

using namespace std;

int main(){
    double x=2;
    cout << setprecision(numeric_limits<double>::max_digits10) << sqrt(x);
}
    
22.12.2015 / 20:22
5

You can do this:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double num = sqrt(2);
    cout << fixed;
    cout.precision(52);
    cout << num << endl;
    return 0;
}

See running on ideone .

    
22.12.2015 / 20:30