Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

c++, should I create my own Complex Numbers class?

This topic is 4054 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 need to use complex numbers, and I've found that there is a std::complex class, but it doesn't have any sqrt function for it, so should I just create my own one, or is there a sqrt function for complex numbers hidden away somewhere in the std library? thx

Share this post


Link to post
Share on other sites
Advertisement
I think <complex> specialises std::sqrt() to work with it.

Remember the SC++L often uses free functions where some may expect member functions.

Even if it didn't instead of re-writing the entire class you could write a free function to compute the square root, much faster [smile]

Share this post


Link to post
Share on other sites
#include <iostream>
#include <complex>
#include <cmath>

int main () {
using namespace std;
complex<float> f(2,2);
cout << sqrt(f) << endl;
}



(1.55377,0.643594)
Press any key to continue . . . _


Works here (tm)

Share this post


Link to post
Share on other sites
Why did you include <cmath>? Are you bringing the c standard lib sqrt into scope just so it can be ignored?

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!