Using imaginary number " i ". Dev C++ vs. VS2008!
Hi There!
I created an algorithm that uses imaginary numbers. It is fine on Dev C++, and now I am trying to port to VS2008. I figured out most things, including how to declare complex numbers. However, I've been having an incredible hard time trying to figure how to use the " i " number! For example:
In Dev C++:
Code:
z_cmplx = cexp(I * f1/Fs * 2 * PI);
Where "I" is a macro from the library!
In VS2008:
Code:
z_cmplx = std::exp(I * f1/Fs * 2 * PI);
Although I DID include <complex> library just like I did before, the compiler gives me: error C2065: 'I' : undeclared identifier.
This is a pretty time sensitive matter, if anyone could help I'd REALLY appreciate! :wave:
Thank you in advance.
Best wishes,
Fernando
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
I'm not an expert on using the complex class, but I believe that for complex numbers, exp works on a complex variable where a complex variable is defined to have a real and imaginary part.
So assuming fl/Fs and PI are real then you would define a complex number
Code:
complex<double> com1(0, fl/Fs * 2 * PI); //This gives an imaginary number ie 0 + i*fl/Fs * 2 * PI
complex<double> com2 = exp(com1);
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Quote:
Originally Posted by
fernando306
Where "I" is a macro from the library!
You have to be joking.
Do you know the number of naming conflicts a 1 letter name would have in a large program? Or how about searching your source files for the usage of the I macro?
Macro names should be named in a descriptive manner, so that conflicts and confusion doesn't occur.
Regards,
Paul McKenzie
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Well the problem comes from an even more basic level...
I am not finding a way to set values to real and imaginary parts of the number separately.
I found that the function real(complex_number) returns the real part of the number, and so does the function imag(complex_number) returns the imaginary part. However I can't set them.
In Dev C++ I do that by using:
__real__ complex_number = desired_real_value
and
__imag__ complex_number = desired_imag_value
However in VC++ 2008 I've been trying for hours to find a way and I still havent found! MSDN documentation only shows how to RETURN the values, not to set them. Been having a hard time here... Anyone knows it??
Thanks.
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
I found no problem setting the values when declaring the complex numbers btw. But after has been declared, I didn't find a way to change the imaginary part of it. In therms of computational efficiency I don't think that it's a good idea do declare it all the time to be able to set values...
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Quote:
Originally Posted by
fernando306
I found no problem setting the values when declaring the complex numbers btw. But after has been declared, I didn't find a way to change the imaginary part of it. In therms of computational efficiency I don't think that it's a good idea do declare it all the time to be able to set values...
Do you mean setting real and imaginary parts separately like this?
Code:
#include <iostream>
#include <complex>
using namespace std;
int main()
{
const double fl = 2, Fs = 3;
const double PI = 3.1416;
typedef complex<double> Compnum;
Compnum com1(0, fl/Fs * 2 * PI); //This gives an imaginary number ie 0 + i*fl/Fs * 2 * PI
Compnum com2 = exp(com1);
Compnum com3 (3, 4);
cout << com3 << endl << com2 << endl;
com3 = Compnum(real(com3), 6);
com2 = Compnum(2, imag(com2));
cout << com3 << endl << com2 << endl;
return 0;
}
This produces the output
Code:
(3,4)
(-0.499992,-0.86603)
(3,6)
(2,-0.86603)
where the imaginary part of com3 has been set directly keeping the real part and the real part of com2 has been set directly keeping the imaginary part.
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
>> Where "I" is a macro from the library!
"I" is part of C99, which VC2008 does not support.
Perhaps you could create a global complex<double> I.
gg
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Quote:
Originally Posted by
Codeplug
>> Where "I" is a macro from the library!
"I" is part of C99, which VC2008 does not support.
Perhaps you could create a global complex<double> I.
gg
I hope that "I" is a keyword and not a macro, as the OP suggested.
Regards,
Paul McKenzie
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Quote:
Originally Posted by
Codeplug
Yikes. I wonder if the language authors were tired that day?
Regards,
Paul McKenzie
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Quote:
Originally Posted by
2kaud
Do you mean setting real and imaginary parts separately like this?
Code:
#include <iostream>
#include <complex>
using namespace std;
int main()
{
const double fl = 2, Fs = 3;
const double PI = 3.1416;
typedef complex<double> Compnum;
Compnum com1(0, fl/Fs * 2 * PI); //This gives an imaginary number ie 0 + i*fl/Fs * 2 * PI
Compnum com2 = exp(com1);
Compnum com3 (3, 4);
cout << com3 << endl << com2 << endl;
com3 = Compnum(real(com3), 6);
com2 = Compnum(2, imag(com2));
cout << com3 << endl << com2 << endl;
return 0;
}
This produces the output
Code:
(3,4)
(-0.499992,-0.86603)
(3,6)
(2,-0.86603)
where the imaginary part of com3 has been set directly keeping the real part and the real part of com2 has been set directly keeping the imaginary part.
Exactly! It worked perectly now. Thanks a lot!!!