So, I am trying to return the square root of a number in two ways, which in my view are not so different. Which of these would have a higher accuracy and why? As far as I can tell the second would be the better one, as I believe the default "allowed error" (not sure how that is called) is much lower than my set variable 'eps': 0.00001. Is that correct? Does this have to do with Codeblocks or the compiler or the processor itself?
First method :
#include <iostream>
using namespace std;
int main()
{
double a,b;
double x;
cin>>x;
int nr=0;
//double eps=0.00001;
a=1;
do
{
b=a;
a=(b+x/b)/2;
nr++; //used to keep track of number of steps made
}while(a!=b);
cout<<a;
return 0;
}
Second method would be the same, except for the do...while() condition:
#include <iostream>
using namespace std;
int main()
{
double a,b;
double x;
cin>>x;
int nr=0;
double eps=0.00001;
a=1;
do
{
b=a;
a=(b+x/b)/2;
nr++;
}while(a-b>=eps || b-a>=eps);
return 0;
}