• Post Reply Bookmark Topic Watch Topic
  • New Topic

StackOverflowError from Recursion  RSS feed

 
Kevin Farmer
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a method to calculate powers, but when I test the method I get a java.lang.StackOverflowError. I've looked up the error and it is most likely from the recursion, however I'm not sure where to fix it.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...you can always stick System.out.println statements in your method to see what it is REALLY doing.

I've not worked through it, but what's happening is you are never reaching your exit condition.  x is somehow never reaching 0.  So print out what x is each time you enter your power method and see what's happening.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try to calculate 1.0 ^ 2 using your method.

1.0 ^ 2 will be calculated as (1.0 ^ 1) ^ 2 (second if statement).
(1.0 ^ 1) will be calculated correctly and the result would be 1.0.

So we'll end up with 1.0 ^ 2.

1.0 ^ 2 will be calculated as (1.0 ^ 1) ^ 2 (second if statement).
(1.0 ^ 1) will be calculated correctly and the result would be 1.0.

So we'll end up with 1.0 ^ 2.

1.0 ^ 2 will be calculated as (1.0 ^ 1) ^ 2 (second if statement).
(1.0 ^ 1) will be calculated correctly and the result would be 1.0.

So we'll end up with 1.0 ^ 2... etc until you get StackOverflowError.

fred rosenberger wrote:I've not worked through it, but what's happening is you are never reaching your exit condition.  x is somehow never reaching 0.

x is reaching 0. The problem is in the second if statement as described above.
 
Kevin Farmer
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when I printed out what x was, the results kept repeating over and over again. With the second if statement I have, shouldn't it go to the next return statement if x % 2 does not equal 0?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Farmer wrote:So when I printed out what x was, the results kept repeating over and over again. With the second if statement I have, shouldn't it go to the next return statement if x % 2 does not equal 0?


Sure it should, if it can ever get there. But look at this code:



That looks like it should do what you want. But let's simplify it a bit:



Give that a try and you'll find you still have the stack overflow problem. So: why doesn't the expression power(1.0, 2) simply return 1?
 
Kevin Farmer
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I know a for loop would have this issue if the loop had nothing telling it to stop. Would I need to create a for loop and then insert the if statement inside it? And then I could put "break;" to exit the if statement. Correct me if I am wrong.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me put it more plainly. Every time you compute b^2, you first evaluate b^1 and then square it... in other words you can't compute b^2 unless you first compute b^2.

Notice that you don't have this problem with b^4 or b^6 or b^any other even power. So b^2 acts differently than all of those other even powers. Therefore you should handle it differently in your code.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!