Results 1 to 15 of 15
Thread: how to end infinite loop
 10252010, 02:41 AM #1Member
 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);????
many thanks in advance
Java 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); } } }
 10252010, 02:49 AM #2
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.
 10252010, 02:58 AM #3Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
You might want to terminate even if the old and new guesses are close enough to the same.
 10252010, 03:10 AM #4
Well, here's my output using a ridiculously large nonsquare 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.
 10252010, 03:14 AM #5Member
 Join Date
 Apr 2010
 Posts
 22
 Rep Power
 0
 10252010, 03:23 AM #6Member
 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) { 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); } } }
 10252010, 03:58 AM #7
Yup. That'll do it. Personally I did it with a second variable, but your method works just as well.
Thanks for sharing your solution.
 10252010, 03:59 AM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
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.
 10252010, 04:53 AM #9Member
 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?
 10252010, 05:10 AM #10Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
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; 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; } } } }
 10252010, 05:28 AM #11Member
 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..
 10252010, 05:35 AM #12
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) {
 10252010, 05:43 AM #13Member
 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..
 10252010, 06:48 AM #14Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
You can also use the absolute value method
OP: You will learn a lot by asking questions (and by offering your own thoughts) at forums like this.
 10252010, 08:59 AM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,879
 Blog Entries
 7
 Rep Power
 21
That fails miserably if oldGuess and guess are both very large, e.g. 1E15 and epsilon is very small, e.g. 1E05. 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(1Math.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
Similar Threads

Infinite loop
By jDennis79 in forum New To JavaReplies: 7Last Post: 08132010, 11:45 PM 
Infinite Loop
By bosoxfan in forum New To JavaReplies: 3Last Post: 02222010, 02:34 AM 
Infinite Loop
By rclausing in forum New To JavaReplies: 2Last Post: 01232010, 11:11 PM 
Infinite running loop problems
By BigDummy in forum New To JavaReplies: 5Last Post: 10142009, 06:39 AM 
java recursion infinite loop
By tony404 in forum Advanced JavaReplies: 9Last Post: 10032008, 01:16 PM
Bookmarks