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>
usingnamespace 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<<" ";}
}