attempted to write a method for finding the square root. it seems to have done everything correctly, but at startup it gives an error:

Exception in thread "main" java.lang.StackOverflowError at hello.Sqrt.midIter(Sqrt.java:23) at hello.Sqrt.midIter(Sqrt.java:23) at hello.Sqrt.midIter(Sqrt.java:23) at hello.Sqrt.midIter(Sqrt.java:23) at hello.Sqrt.midIter(Sqrt.java:23) 

The program code is as follows:

  public static double middler(double arg1, double arg2){//находит число, стоящее в средине диапазона заданных в аргументах значениях double res = (arg2+(arg1-arg2)/2); return res; } /**/ private static double midIter(double sqr, double app, double low){//метод уменьшает диапазон (как в двоичном поиске) пока квадрат средины диапазона не будет входить в пределы погрешности double mid = middler(app, low);//нахождение средины диапазона double sq=mid*mid;//возведения средины диапазона в квадрат boolean ss = ((sqr-0.01d)<sq && sq<(sqr+0.01d));//проверка вхождения квадрата в диапазон допустиых значений if(ss)//если квадрат входит в диапазон допустимых значений, возвращается найденное число return mid; else{//если же нет, диапазон, заданный аргументами, делится на две части и происходит поиск в более подходящей из них if(sq>sqr) return midIter(sqr, sq, low);//вызывется этот же метод (рекурсия) с новыми значениями диапазона else return midIter(sqr,app, sq);//вызывется этот же метод (рекурсия) с новыми значениями диапазона } } public static double sQrt(double arg){//метод, который вызывается первым и вызывает все остальные. в качестве аргумента принимает число, корень которого нужно найти double apBound = arg;//верхняя граница double lBound = 0f;//нижняя граница double m = middler(apBound, lBound); if(m*m==arg) return m; else{ return midIter(arg, apBound, lBound); } } 
  • Maybe too deep recursion occurs? The stack is not rubber. - zed
  • @zed, and how to increase the stack or reduce the depth of recursion? - KVV
  • You can increase the stack like this: codeforces.com/blog/entry/166?locale=en and the depth of recursion can be changed only by changing the logic of your algorithm. But for starters, it would not be bad to know the current recursion depth cyberforum.ru/java/thread822287.html - zed
  • one
    Author, you have been tasked with using recursion or not? If not, then the author of the book "Perfect Code" Steve McConnell responded about recursion in a similar fashion: recursion is a very powerful development tool, and you always need to consider alternative versions and use them if you can do without recursion. By the way, using recursion to calculate factorial is not the best option. - Vyacheslav Martynenko
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

It seems to me that your method will converge much faster, and, accordingly, such a deep stack will not be needed if you write the corresponding part of the code as follows:

 if(sq>sqr) return midIter(sqr, mid, low); else return midIter(sqr,app, mid); 

That is, replace sq by mid . Then at each iteration the segment will be halved.

Generally, to be honest, I have concerns that if you leave everything as it is, the root may not be found at all, since the segment may not decrease at all. And then no stack increase will help.

Usually such tasks are solved without recursion using a while () while .