I am getting a stackoverflow error when I run my program. I am quite knew to java and I could use a bit of advice.
Any help is appreciated!
public class ApproxSquareRoot {
public static float sqrRootRec(float number, float approx, float tol) {
if((Math.abs((Math.pow(approx,2) - number))<= tol*number)) {
return approx;
} else
return sqrRootRec(number, (approx*approx + number/(2*approx)),tol);
}
public static float sqrRoot(float number, float approx, float tol) {
while (Math.abs((Math.pow(approx,2) - number))<= tol*number)
approx = (approx*approx + number)/(approx + approx);
return approx;
}
}
.
Input number: 43
Input approx: 2.5
Input tol: .1
Output with number = 43.0 approx = 2.5 tolerance = 0.1
Exception in thread "main" java.lang.StackOverflowError