JAVA Help: Stuck in Loop

Discussion in 'OT Technology' started by Ag330Ci, Feb 25, 2007.

  1. Ag330Ci

    Ag330Ci OT Supporter

    Joined:
    Aug 31, 2003
    Messages:
    1,107
    Likes Received:
    0
    Ok, in the equation z^y = x, I'm trying to find value of z(closest to .001)when values of y and x are given. It keeps getting stuck in a loop, and I don't know what the problem is.

    Code:
    public void process( )
    	{
    		double upperBound = x;
    		double lowerBound = 1;
    		int countY = 0;
    		xGuess = 1;
    		
    		do
    		{
    		zGuess = (upperBound + lowerBound)/2;
    			while (xGuess < x && countY < y)
    			{
    				xGuess = zGuess * zGuess;
    				countY++;
    			}
    			if (xGuess > x)
    				upperBound = zGuess;
    			else lowerBound = zGuess;
    			if (x > xGuess)
    				difference = x - xGuess;
    			else difference = xGuess - x;
    			
    		}while (difference > .001);
    		
    		System.out.println("Answer is: " + zGuess);
    	}
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    use the math.lang package's logs and exponents

    double power = Math.log(x) / y;
    z = Math.exp(power);

    i think that math is right :hs:
     
  3. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    2^3 = 8
    log base2 of 8 = 3

    i think thats the way it goes. did i get my math right? whats this while loop for?
     

Share This Page