Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

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

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    You might want to terminate even if the old and new guesses are close enough to the same.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

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

    Default

    Quote Originally Posted by Zack View Post
    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. #6
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

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

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

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

    Default

    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. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

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

  11. #11
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    :D

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

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

  12. #12
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

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

    Default

    thanks fellows
    :D
    i want one of you to be my mentor..i am working in sales... desperately wanting transition in programming..

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    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

Similar Threads

  1. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-14-2010, 12:45 AM
  2. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 02:34 AM
  3. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 11:11 PM
  4. Infinite running loop problems
    By BigDummy in forum New To Java
    Replies: 5
    Last Post: 10-14-2009, 07:39 AM
  5. java recursion infinite loop
    By tony404 in forum Advanced Java
    Replies: 9
    Last Post: 10-03-2008, 02:16 PM

Posting Permissions

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