Jump to content
  • Advertisement
Sign in to follow this  
alway616

square roots with C++

This topic is 4251 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

how do i use them? i need them for a program i am making, but they aren't covered in the C++ book i have i tried making it by using X^.5 but it just generates an error

Share this post


Link to post
Share on other sites
Advertisement
Note that although X ^ 2 will compile, it is not a power operation.

^ is a bitwise XOR operation.

To use powers of, you can use pow() from <cmath>, or for simple powers just write it out longhand ( eg: for X cubed use (X * X * X) )

Share this post


Link to post
Share on other sites
Just a note... the sqrt and pow functions are extremely slow (as they do huge number of multiplications and divisions), so if you have to use them a lot (such as in a loop), try to avoid them if possible.

Share this post


Link to post
Share on other sites
I'll have to disagree, marshdabeachy.

On current hardware, the sqrt and pow functions aren't that slow, and they certainly don't perform many arithmetic operations.

While these particular floating-point operations are relatively slow compared to integer arithmetic, they are lightning-fast compared to any alternative implementation (especially on SSE hardware and the likes). If you need to calculate a square root or calculate a power, you won't do any better than these functions unless you sacrifice a great deal of accuracy. Considering that you can reel off billions of sqrt()s per second, I really wouldn't worry about micro-optimisations like this.

Regards
Admiral

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!