# pow problem: 3^2 = 8 !!!

• ### Question

• Hi!

I'm using C++ Amp and have troubles with fast_math::pow function.

fast_math::pow(3,2)  gives me 9 correctly

fast_math::pow( 3, exp ) where exp is a variable passed from C# and set to 2 gives me 8!.

What's the reason for this?

If you want i can send you my VS solution, just tell me where to send.

Thank you.

Mauro

• Edited by Sunday, September 15, 2013 1:50 PM
Sunday, September 15, 2013 1:50 PM

• Seems that the result is 8.999..., which then is truncated by your code to int, giving 8. The function precise_math::powf( 3, i ) appears to work better. Or try this:

`int resIndex = fast_math::round( fast_math::pow( 3, i ));`

Wednesday, September 18, 2013 8:16 AM

### All replies

• Have you checked (with Debugger) that exp contains 2 before executing pow?

A sample project can be posted to http://skydrive.live.com.

• Edited by Monday, September 16, 2013 5:35 AM
Monday, September 16, 2013 5:34 AM
• HI Viorel,

https://skydrive.live.com/redir?resid=82734269B54D4F9A!137

I hope it's my fault somewhere in that 10 lines of code. It must be me, but i can't find out.

I wait for your feedback

Thank you

Mauro

Tuesday, September 17, 2013 8:06 PM
• Seems that the result is 8.999..., which then is truncated by your code to int, giving 8. The function precise_math::powf( 3, i ) appears to work better. Or try this:

`int resIndex = fast_math::round( fast_math::pow( 3, i ));`

Wednesday, September 18, 2013 8:16 AM
• Hi, this solved the problem however why this problem do not show with constants or local variables? Thank you

Mauro

Friday, September 20, 2013 8:42 PM
• Hi, this solved the problem however why this problem do not show with constants or local variables?

Probably the compiler performs some optimisation in case of known values.

Sunday, September 22, 2013 7:06 AM