I have a c++ algorithm that calculates the square root of an integer. The program works with the exception of a single flaw. It is unable to calculate the square root of a number that is below 1. For example, it cant calculate the square root of .5 or .9 or .0000001 etc. but works as planned for all other situations. I have X set so it doesn't allow a negative input, but I still can't see why it wont return a value for anything less than 1.
include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
double squareroot(double x)
{ /* computes the square root of x */
/* make sure x is not negative .. no math crimes allowed! */
assert(x >= 0);
if (x == 0) return 0;
/* the sqrt must be between xhi and xlo */
double xhi = x;
double xlo = 0;
double guess = x / 2;
/* We stop when guess*guess-x is very small */
while (abs(guess*guess - x) > 0.000001)
{
if (guess*guess > x) xhi = guess;
else xlo = guess;
guess = (xhi + xlo) / 2;
}
return guess;
}
/* Test Stub */
int main()
{
double testvalue;
cout << "\n Enter a TestValue= ";
cin >> testvalue;
cout << endl;
double testresult = squareroot(testvalue);
cout << "\n Square Root= " << testresult << "\n";
}
Thanks for the help! I was able to solve the problem by using the following:
if (x<1) {
xhi = 1;
xlo = x;
guess = (x + 1) / 2;
}