Show more accuracy in C++

Asked

Viewed 1,071 times

6

I have this program:

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

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

2 answers

7


Utilizes setprecision in 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);
}
  • 2

    It works. But is there any way to get the maximum precision?

  • @Alephzero Yes, there is. You have to use numeric_limits<double>. I’ve already edited.

6

  • One question, whether the 2008 IEEE 754 standard, which defines the representation of double-precision floating-point numbers (64 bits), provides a relative accuracy of about 16 decimal digits and a magnitude range of 10 -308 to 10 +308, where does this number come from? For greater precision it would not be necessary to use one of the libraries that implement arbitrary precision decimal number representations (e.g., GMP - The GNU Multiple Precision Arithmetic Library ) and not the double data type?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.