# A little help

• October 24th, 2009, 05:50 PM
Mekster
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.

Code :

``` 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); }   }```
• October 24th, 2009, 06:24 PM
copeg
Re: A little help
According to your code, you have a while loop that is essentially
Code :

```while(true){ // }```
because you never change the closeEnough variable. Possible fix:
Code :

```if ( (XX - X)/X < 0.00001) { break; }```
use a break statement.
• October 24th, 2009, 06:46 PM
Mekster
Re: A little help
Thank you seems to have opened some doors. Thanks.