This week's book giveaway is in the Agile and Other Processes forum.We're giving away four copies of Software Teaming: A Mob Programming, Whole-Team Approach and have Woody Zuill and Kevin Meadows on-line!See this thread for details.
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Tim Cooke
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Paul Clapham
• Rob Spoor
• Junilu Lacar
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Piet Souris
• Carey Brown
Bartenders:

Power method help(recursive)

Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
Ok I have this recursive method which evalutes the value of positive exponents fine, but I need it to work with negative exponents, and im not sure how to do this. This is the code I have now

Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
I think the easiest thing to do would be to simply evaluate the power and then divide 1 by the power. If you're going to have large powers I would recommend using BigInteger or BigDecimal.

author
Posts: 23930
142
• Number of slices to send:
Optional 'thank-you' note:
It is actually pretty straightforward. Here is a hint...

Is there a way to calculate a value to a negative exponent based on knowing the value to a positive exponent? If there is, can you recursively calculate the value to a positive exponent, and use that to calculate the value to the negative exponent?

Henry

Steve Shipman
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
Doh, didnt even think of the 1 divided by the power thing. But unfortunatly that wont work either, I had this

but it gives me the same problem as the original, just goes into a stack over flow exception.

Henry Wong
author
Posts: 23930
142
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Ikara Lott:
Doh, didnt even think of the 1 divided by the power thing. But unfortunatly that wont work either, I had this

but it gives me the same problem as the original, just goes into a stack over flow exception.

Close... two more hints...

1. You don't need to have a special negative power method, you can do the whole thing recusively within the power method.

2. When you "1 divided by the power thing", don't you also have to change the sign?

Henry

Steve Shipman
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
ohhhhh change the sign! Haha yeah that might help.

Ok well I tried this:

but that was a no go, same error
[ March 28, 2006: Message edited by: Ikara Lott ]

Henry Wong
author
Posts: 23930
142
• Number of slices to send:
Optional 'thank-you' note:
When I said that you can do the whole thing with one power() method, I meant that you have to do it without breaking the method. Try it with positive numbers, you'll see that it no longer works...

Henry

Steve Shipman
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
hmmm ok I tried this for the last return statement, isntead of what I had above

And it will work with finding the regular exponents, like if I do 2^-2, ill get 4. However when I add the 1 divded by the power part

I dont get the error, but I dont get the correct answer either 2^-3 gives me .5

Keith Lynn
Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
The second method should not be recursive. It should look more like this

Note that when you have a negative power you don't change the sign of the number.

[ March 28, 2006: Message edited by: Keith Lynn ]
[ March 28, 2006: Message edited by: Keith Lynn ]

Steve Shipman
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
"When I said that you can do the whole thing with one power() method, I meant that you have to do it without breaking the method. Try it with positive numbers, you'll see that it no longer works..."

Oh yeah, I didnt break it into one method yet, im just trying to get the negative ones to work. Hehe I did see it didnt work with positive ones anymore, but im just getting the negative exponent thing worked out first

Steve Shipman
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
"The second method should not be recursive. It should look more like this"

Thats part of the way I have to do it though, I have to have it evaluate negative exponents recursivly

Henry Wong
author
Posts: 23930
142
• Number of slices to send:
Optional 'thank-you' note:

Oh yeah, I didnt break it into one method yet, im just trying to get the negative ones to work. Hehe I did see it didnt work with positive ones anymore, but im just getting the negative exponent thing worked out first

It's a recursive algorithm, the negative calls depends on the positive calls. The negative exponents will not work, if the positive exponents do not work.

Henry

Steve Shipman
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
ahh I see, yeah when I split it back up into 2 methods it works fine, now ill just go back and make it one method. Thanks for your help guys

Keith Lynn
Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
Here is one way of looking at it.

Whether it's a negative or positive power, the stopping point will be 0.

Note the following

x^3 = x * x^2

x^-3 = x^-1 * x^-2

[ March 28, 2006: Message edited by: Keith Lynn ]

Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Ikara Lott:
...

By the way, nice name.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.