
August 13th, 2013, 04:47 PM
#1
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!
Thank you in advance.
Best wishes,
Fernando

August 13th, 2013, 05:58 PM
#2
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);
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

August 13th, 2013, 07:49 PM
#3
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
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

August 13th, 2013, 09:35 PM
#4
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.

August 13th, 2013, 09:56 PM
#5
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...

August 14th, 2013, 06:11 AM
#6
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
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.
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

August 14th, 2013, 10:43 AM
#7
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

August 14th, 2013, 01:35 PM
#8
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
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

August 14th, 2013, 03:31 PM
#9
Re: Using imaginary number " i ". Dev C++ vs. VS2008!

August 14th, 2013, 03:40 PM
#10
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
Originally Posted by Codeplug
Yikes. I wonder if the language authors were tired that day?
Regards,
Paul McKenzie

August 15th, 2013, 01:01 PM
#11
Re: Using imaginary number " i ". Dev C++ vs. VS2008!
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!!!
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
