Using imaginary number " i ". Dev C++ vs. VS2008!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Using imaginary number " i ". Dev C++ vs. VS2008!

  1. #1
    Join Date
    Jun 2012
    Posts
    37

    Question 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

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Using imaginary number " i ". Dev C++ vs. VS2008!

    Quote Originally Posted by fernando306 View Post
    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

  4. #4
    Join Date
    Jun 2012
    Posts
    37

    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.

  5. #5
    Join Date
    Jun 2012
    Posts
    37

    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...

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: Using imaginary number " i ". Dev C++ vs. VS2008!

    Quote Originally Posted by fernando306 View Post
    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.

  7. #7
    Join Date
    Nov 2003
    Posts
    1,800

    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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Using imaginary number " i ". Dev C++ vs. VS2008!

    Quote Originally Posted by Codeplug View Post
    >> 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

  9. #9
    Join Date
    Nov 2003
    Posts
    1,800

    Re: Using imaginary number " i ". Dev C++ vs. VS2008!

    It's a macro.

    See 7.3.1 - 4 (p170)
    http://www.open-std.org/jtc1/sc22/WG...docs/n1256.pdf

    gg

  10. #10
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Using imaginary number " i ". Dev C++ vs. VS2008!

    Quote Originally Posted by Codeplug View Post
    It's a macro.

    See 7.3.1 - 4 (p170)
    http://www.open-std.org/jtc1/sc22/WG...docs/n1256.pdf

    gg
    Yikes. I wonder if the language authors were tired that day?

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Jun 2012
    Posts
    37

    Re: Using imaginary number " i ". Dev C++ vs. VS2008!

    Quote Originally Posted by 2kaud View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center