Answered by:
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 Heartbreaka Sunday, September 15, 2013 1:50 PM
Answers

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 ));
 Proposed as answer by Łukasz MendakiewiczMicrosoft employee, Moderator Friday, September 20, 2013 8:14 PM
 Marked as answer by Heartbreaka Friday, September 20, 2013 8:40 PM
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 Viorel_MVP Monday, September 16, 2013 5:35 AM


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 ));
 Proposed as answer by Łukasz MendakiewiczMicrosoft employee, Moderator Friday, September 20, 2013 8:14 PM
 Marked as answer by Heartbreaka Friday, September 20, 2013 8:40 PM

