1 / ( 2 ^ n )

Started by
9 comments, last by NotAYakk 17 years, 12 months ago
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?
Advertisement
Er, why not just use pow()?
1.0f / pow(2.0f, n);

Should work nicely :)
Or better yet, pow(2,-n).
Math.h is your freind!

Unless this isn't C++...
Couldn't you do something like...
1.0f / (1 << n)

Although, this wouldn't work if n wasn't an integer.
Oh my god! I can't believe I've never heard of pow()! Thanks a lot you guys have saved my skin!
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.
----------------------------

http://djoubert.co.uk
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.
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.

This topic is closed to new replies.

Advertisement