# 1 / ( 2 ^ n )

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

## 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 on other sites
Er, why not just use pow()?

##### Share on other sites
1.0f / pow(2.0f, n);

Should work nicely :)

##### Share on other sites
Or better yet, pow(2,-n).

##### Share on other sites
Math.h is your freind!

Unless this isn't C++...

##### 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 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 on other sites
Quote:
 Original post by deej21Couldn'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 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 on other sites
Quote:
 Original post by StormArmageddonIf 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634089
• Total Posts
3015458
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!