Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: A little help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A little help

    So I have an infinite loop on my hands, and I was wondering if anyone can help me out. My purpose is to make an iterative cube root function using newtons method. Any help would be greatly appreciated.

    I set a = 1.0 just to test. I need to go through numbers 1 -50 but thats really irrelavant.

        public static void cRoot ()
        {
           ///newtons method for compution.
          boolean closeEnough = false;
          double X, XX;
          double a = 1.0;
           /// set the cubeRoot for 0 and 1.
           if (a == 0.0)
               X = 0.0;
            if (a == 1.0)
               X = 1.0;
     
           X = a/2.0;  // first guess.
           while( closeEnough == false )
           {
                // Next guess
                XX = (1/3.0 *( 2.0 * Math.pow(X, 3) + a) / (Math.pow(X, 2)));
                // Is it close enough?
                if ( (XX - X)/X < 0.00001)
                 // New guestimate.
                X = XX;
           }
           /// Output of the cuberoot.
           System.out.print(X);
        }
     
    }
    Last edited by helloworld922; October 25th, 2009 at 01:01 AM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: A little help

    According to your code, you have a while loop that is essentially
    while(true){
    //
    }
    because you never change the closeEnough variable. Possible fix:
    if ( (XX - X)/X < 0.00001) 
    {
        break;
    }
    use a break statement.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A little help

    Thank you seems to have opened some doors. Thanks.
    Last edited by Mekster; October 24th, 2009 at 07:48 PM.