# Thread: how to end infinite loop

1. Member
Join Date
Apr 2010
Posts
22
Rep Power
0

## 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);????

Java Code:
```package worksheet;

import java.util.Scanner;

public class roughwork2
{
public static void main(String[] args)

{

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);
}

}
}```

2. Senior Member
Join Date
Jun 2010
Location
Destiny Islands
Posts
690
Rep Power
0
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.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
You might want to terminate even if the old and new guesses are close enough to the same.

4. Senior Member
Join Date
Jun 2010
Location
Destiny Islands
Posts
690
Rep Power
0
Well, here's my output using a ridiculously large non-square number:
Java 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).

5. Member
Join Date
Apr 2010
Posts
22
Rep Power
0
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

6. Member
Join Date
Apr 2010
Posts
22
Rep Power
0
i think i figured it out... is this the right way to do it..

Java Code:
```import java.util.Scanner;

public class roughwork2
{
public static void main(String[] args)

{

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);
}

}
}```

7. Senior Member
Join Date
Jun 2010
Location
Destiny Islands
Posts
690
Rep Power
0
Yup. That'll do it. Personally I did it with a second variable, but your method works just as well.

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
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.

9. Member
Join Date
Apr 2010
Posts
22
Rep Power
0
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
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:

Java Code:
```import java.util.Scanner;

public class Roughwork2
{
public static void main(String[] args)
{
double epsilon = 0.000001;
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;
}
}
}
}```

11. Member
Join Date
Apr 2010
Posts
22
Rep Power
0
:D

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

i would have never thought anything in that line in next 100 years..

12. Senior Member
Join Date
Jun 2010
Location
Destiny Islands
Posts
690
Rep Power
0
You can also use the absolute value method:
Java 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.

13. Member
Join Date
Apr 2010
Posts
22
Rep Power
0
thanks fellows
:D
i want one of you to be my mentor..i am working in sales... desperately wanting transition in programming..

14. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
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.

15. Originally Posted by Zack
You can also use the absolute value method:
Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•