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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



Thanks in advance!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic