Not precise enough digits

I have to use Heron's formula to find a close number to the square root up to (at least!) 7 digits after decimal preciseness. However, even double or long double is not enough. Any suggestions? (I know a vertex has infinite storage size, but surely that wouldn't work..?)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main() {
    double a, d, val, steps, r;
    cin>>a;
    for (int i=0;i<a;i++){
        r=1; 
        cin>> val>>steps; // To find square root r of a value v, first let r = 1;
       for (int i=0; i<steps;i++)
       {d= val/r; // Calculate d as d = v / r - obviously the better approximation r gives, the closer d will be to it;
        r=(r+d)/2;}       //Calculate average between r and d and use it as the next step approximation, i.e. r{new} = (r + d) / 2);
             cout<<r<<" ";}
}


Output: 17.3781 8.544 302.544 99.3831 6.63325 9.23724 8555 53.5756 21.6564 28.8235 96.1034
Expected: 17.378147197 8.54400374532 302.543827872 99.3830973775 6.63324958071 9.23723724426 8554.99997077 53.5756330375 21.6564078277 28.8235465243 96.1034351588
Last edited on
Have you used setprecision() before?

Maybe that will help. Used in conjunction with "fixed" you should be able to say that you want the double to go to 7 decimal places.

http://www.cplusplus.com/reference/iomanip/setprecision/
huzzah! Thanks. :D
Topic archived. No new replies allowed.