• Advertisement
Sign in to follow this  

1 / ( 2 ^ n )

This topic is 4326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've hit a point in my project where I need to implement a formula and I can't see how I'm gonna do this one. The formula (as it says on the tin ) is: 1 / ( 2 ^ n ) ( That is one over two to the power of n ) where n is any of the set of real numbers. I'd consider a recurring function, but n isn't necessarily ( or even likely to be ) a whole number. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Er, why not just use pow()?

Share this post


Link to post
Share on other sites
Or better yet, pow(2,-n).

Share this post


Link to post
Share on other sites
Couldn't you do something like...
1.0f / (1 << n)

Although, this wouldn't work if n wasn't an integer.

Share this post


Link to post
Share on other sites
Oh my god! I can't believe I've never heard of pow()! Thanks a lot you guys have saved my skin!

Share this post


Link to post
Share on other sites
Quote:
Original post by deej21
Couldn't you do something like...
1.0f / (1 << n)

Although, this wouldn't work if n wasn't an integer.


Very original, I wonder what happends if N is a a negative number. Would the compiler actually shift it to the right instead, I don't think so.

Share this post


Link to post
Share on other sites
If your using fixed point and assuming n>0 you could do (1>>n) where 1 would be the fixed point equivilant. Plus this would be really fast.

Share this post


Link to post
Share on other sites
Quote:
Original post by StormArmageddon
If your using fixed point and assuming n>0 you could do (1>>n) where 1 would be the fixed point equivilant. Plus this would be really fast.


It should work for n >= 0.

Share this post


Link to post
Share on other sites
Approximate your real number with a series of rational numbers that converge to your real number.

Then for each of these approximations, implement a Taylor series for k^x:
http://mathworld.wolfram.com/TaylorSeries.html

Finally, execute the taylor series that approximates your result to the degree of precision you want.

Easy!







(I dare you to try it. (; )

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement