Kevin Farmer

Greenhorn

Posts: 14

posted 10 months ago

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.

posted 10 months ago

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.

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 10 months ago

Let's try to calculate

So we'll end up with

So we'll end up with

So we'll end up with

x is reaching 0. The problem is in the second if statement as described above.

`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

posted 10 months ago

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

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

posted 10 months ago

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.

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.

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |