# how to end infinite loop

• 10-25-2010, 03:41 AM
search4survival
how to end infinite loop
hi i have a question. i am trying to find square root through Babylonian method.

so far i have done this ....it works fine sometimes but it goes to infinite loop printing the same value again and again.

is there any way i can tell compiler if i am printing same output please abort??

or i can check every 5 lines..if i am producing same output then please System.exit(0);????

Code:

```package worksheet; import java.util.Scanner; public class roughwork2 {         public static void main(String[] args)                         {                                 System.out.println("Please enter a number");                 Scanner readinput = new Scanner(System.in);                 double number  = readinput.nextDouble();                 double guess = number/2;                                                         for(double i = 0 ; ((number)/Math.pow(guess, 2) != (int) 1 ); i++)                 {                                                                 guess        = (( number/guess ) + guess) / 2 ;                                 System.out.printf("% ,.2f\n", guess);                         }                                         }                }```
• 10-25-2010, 03:49 AM
Zack
I would just check against the last value to see if they're the same. If they are there's a safe bet the loop will not terminate.

Compare the result of "((number/guess) + guess) / 2" to the old value of "guess". If they're equal, end the loop.
• 10-25-2010, 03:58 AM
pbrockway2
You might want to terminate even if the old and new guesses are close enough to the same.
• 10-25-2010, 04:10 AM
Zack
Well, here's my output using a ridiculously large non-square number:
Code:

```Please enter a number 2729472947714 682,368,236,929.500000 341,184,118,466.750000 170,592,059,237.375000 85,296,029,626.687500 42,648,014,829.343750 21,324,007,446.671875 10,662,003,787.335938 5,331,002,021.667968 2,665,501,266.833976 1,332,751,145.416922 666,376,596.707937 333,190,346.349772 166,599,269.141317 83,307,826.302116 41,670,295.002973 20,867,898.324915 10,499,348.008695 5,379,656.977836 2,943,513.153104 1,935,398.627010 1,672,844.214825 1,652,240.138621 1,652,111.668205 1,652,111.663210 Finished.```
I think it's a safe bet that he can terminate the loop if the last one is equal. If he wanted to use accuracy, though, he'd just have to compare abs(old - new) <= 0.0001 (or whatever accuracy he wanted).
• 10-25-2010, 04:14 AM
search4survival
Quote:

Originally Posted by Zack
I would just check against the last value to see if they're the same. If they are there's a safe bet the loop will not terminate.

Compare the result of "((number/guess) + guess) / 2" to the old value of "guess". If they're equal, end the loop.

hi thanks for replying both of u

question is how do i compare old value to new value..if i knew that theres no issue..

one of the ways i see is to do array index and say if value of index[i] == index[i-1]
please terminate...i could be wrong though
• 10-25-2010, 04:23 AM
search4survival
i think i figured it out... is this the right way to do it..

Code:

```import java.util.Scanner; public class roughwork2 {         public static void main(String[] args)                         {                                 System.out.println("Please enter a number");                 Scanner readinput = new Scanner(System.in);                 double number  = readinput.nextDouble();                 double guess = number/2;                                                         for(double i = 0 ; ((number/guess) + guess) / 2 != guess  ; i++)                 {                                                                 guess        = (( number/guess ) + guess) / 2 ;                                 System.out.printf("% ,.2f\n", guess);                         }                                         }                }```
• 10-25-2010, 04:58 AM
Zack
Yup. That'll do it. Personally I did it with a second variable, but your method works just as well.

• 10-25-2010, 04:59 AM
pbrockway2
That looks OK (I haven't tested it though).

But you (or, rather, the computer) is doing all the computations twice. If you used a while loop you might be able to get away with doing the calculation just once.

Lines like double1==double2 make me fearful of infinite loops just because of crazy floating point arithmetic. Of course you could do the mathematical analysis to show that this isn't a problem when extracting square roots this way, but the idea of checking the difference of the guesses against some small value seems more robust than checking to see if the guesses are equal.
• 10-25-2010, 05:53 AM
search4survival
thank you both for quick replies...:)

pbrock.. i understand you point.. what are your thoughts about using arrays? just want to know your opinion?
• 10-25-2010, 06:10 AM
pbrockway2
There's no need to use arrays since you are only interested in two values: what your guess is and what your guess was last time. Like Zack I'd probably use a second variable. Something like:

Code:

```import java.util.Scanner; public class Roughwork2 {     public static void main(String[] args)     {         double epsilon = 0.000001;         System.out.println("Please enter a number");         Scanner readinput = new Scanner(System.in);         double number  = readinput.nextDouble();         double guess = number / 2;         while(true)         {             double oldGuess = guess;             guess = (( number/guess ) + guess) / 2 ;             double diff = oldGuess - guess;             System.out.printf("%,.8f\n", guess);             if(diff < epsilon && diff > -epsilon)             {                 break;             }         }     } }```
• 10-25-2010, 06:28 AM
search4survival
:D

oh my god...that's perfect....

i would have never thought anything in that line in next 100 years..
• 10-25-2010, 06:35 AM
Zack
You can also use the absolute value method:
Code:

```            double diff = Math.abs(oldGuess - guess);             System.out.printf("%,.8f\n", guess);             if(diff < epsilon)             {```
Just saves your if statement a little bit. It's personal preference; it works perfectly fine either way.
• 10-25-2010, 06:43 AM
search4survival
thanks fellows
:D
i want one of you to be my mentor..i am working in sales... desperately wanting transition in programming..
• 10-25-2010, 07:48 AM
pbrockway2
Quote:

You can also use the absolute value method
Yes ... absolutely ;) I don't know why I wrote it the way I did.

OP: You will learn a lot by asking questions (and by offering your own thoughts) at forums like this.
• 10-25-2010, 09:59 AM
JosAH
Quote:

Originally Posted by Zack
You can also use the absolute value method:
Code:

```            double diff = Math.abs(oldGuess - guess);             System.out.printf("%,.8f\n", guess);             if(diff < epsilon)             {```
Just saves your if statement a little bit. It's personal preference; it works perfectly fine either way.

That fails miserably if oldGuess and guess are both very large, e.g. 1E15 and epsilon is very small, e.g. 1E0-5. i.e. the difference is too small to be represented relative to thos large numbers. A relative error can (partly) solve this issue: Math.abs(1-Math.abs(oldGuess/guess)) < epsilon but there are a few methods in the Math class that can help: nextUp( ... ) and ulp( ...). They can be used to test whether or not oldGuess and guess are closer together than n*ulp, for small integer value of n.

kind regards,

Jos