My square root program for some reason gets an answer that is a little off from what it should get for most inputs. I'm not sure why this is. Only certain inputs are wrong. I also get a segmentation fault at then very end after the answer is given and am not sure why that is either.
#include<iostream>
#include<cmath>
#include<cfloat>
#include<string>
#include <cstdlib>
using namespace std;
//declare sqroot function calls recursive function newton
double sqroot1(double num );
double newton(double num, double guess);
int main(int argc, char **argv)
{
for( int i = 0 ; i < argc ; i++)
{
cout<<"sqroot("<<argv[i+1]<<") is "<< sqroot1(atoi(argv[i+1])) <<endl;
}
}
double newton(double num, double a)
{
if ((abs(a*a - num) <= FLT_EPSILON))
{
return a;
}
else
{
newton(num, (a+num/a)/2 );
}
}
double sqroot1(double num)
{
double sqrt = newton(num,num/2);
return sqrt;
}