Hi there, I'm working on a c program that generates approximations of square roots based on newton raphson method. I worked out the approximations with another method I learnt and I know this program does fine. But I don't get the concept of epsilon. Below is the code for the program
Code:
#include<stdio.h>
// function to compute absolute value
float absoluteValue(float x)
{
if(x < 0)
x = -x;
return (x);
}
// function to compute the square root of a number
float squareRoot (float x)
{
const float epsilon = .00001; //why is this value chosen?
float guess = 1.0;
while(absoluteValue(guess * guess - x) >= epsilon)
guess = (x/guess + guess) / 2.0;
return guess;
}
int main (void)
{
printf("squareRoot(2.0) = %f\n", squareRoot(2.0));
printf("squareRoot(144.0) = %f\n", squareRoot(144.0));
printf("squareRoot(17.5) = %f\n", squareRoot(17.5));
return 0;
}
The text I worked this code from uses the epsilon value arbitrarily. So can someone help me understand how is the epsilon value selected? I tried using this formula to derive the square root of 45. On paper I can go as far as 6.8 something. But once this value is reached it's still bigger than epsilon, so shouldn't the code keep running? The program generates 1.41 for 2, but since it could go further why does the while loop in squareRoot function terminate?
Thanks in advance and sorry for being verbose too.