Problems with #include<complex.h>

  • Thread starter JorgeM
  • Start date
In summary, the conversation discusses the implementation of complex.h library for calculations in C++ and the confusion surrounding the use of imaginary numbers. The discussion includes errors encountered in using the library and examples of correct usage in both C and C++.
  • #1
JorgeM
30
6
Hi there. I have been trying to implement complex.h library to make some calculations in c++.Anyways I am not sure why mi compiler does not run my code at all.
C:
#include <iostream>
#include <complex.h>
#include <stdio.h>
using namespace std;
int main()
{
    double complex z = CMPLX(0.0, -0.0);
    cout<<" z = "<<creal(z)<<" + " <<cimag(z)<<endl;
    return 0;
}
Codeblocks says:
Error: expected initializer before z
Error: z was not declared in this scope
I am really confused because even in cpp.com this procedure for imaginary numbers is given.

If you could help me with how may I use, I would be really grateful.
Thanks a lot
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
JorgeM said:
double complex z
I don't know where you got that from but that cannot work. You are trying to assign a double called "complex", and then the compiler has no idea what to do with the z.
Here are examples how to do it
complex<double> z
 
  • Like
Likes FactChecker
  • #3
I think you are mixing C and C++ usage. The following compiles and runs in C:

C:
#include <complex.h>
#include <stdio.h>

int main()
{
double complex z = CMPLX(0.0, -0.0);
printf("z = %.1f%+.1fi\n", creal(z), cimag(z));
return 0;
}

The following code compiles and runs in C++:

Code:
#include <iostream>
#include <complex.h>
#include <stdio.h>
using namespace std;
int main()
{
complex<double> z(0.0, 0.0);
cout<<" z = "<<real(z)<<" + " <<imag(z)<<" i"<<endl;
return 0;
}
 
  • Like
Likes jim mcnamara, FactChecker, JorgeM and 1 other person

1. What is the purpose of using #include<complex.h> in a program?

The #include<complex.h> header file is used to define and access functions for complex numbers in the C programming language. It contains functions for performing arithmetic operations, converting between different representations of complex numbers, and other useful operations.

2. Are there any common problems associated with using #include<complex.h>?

Yes, there are a few common problems that programmers may encounter when using #include<complex.h>. These include conflicts with other header files, issues with declaring and using complex variables, and compatibility problems with different compilers.

3. How can I avoid conflicts with other header files when using #include<complex.h>?

To avoid conflicts with other header files, it is recommended to include #include<complex.h> at the top of your program, before any other header files. This will ensure that the complex number functions are properly defined and can be used throughout your program.

4. Can I use #include<complex.h> in a C++ program?

Yes, the #include<complex.h> header file can be used in both C and C++ programs. However, it is important to note that the functions in the header file are defined in the C programming language, so they may not be fully compatible with C++.

5. Are there any alternatives to using #include<complex.h> for working with complex numbers?

Yes, if you are working with C++, you can use the <complex> header file, which provides a more robust and versatile set of functions for working with complex numbers. Additionally, there are other libraries and packages available that offer advanced features for working with complex numbers in both C and C++ programs.

Similar threads

  • Programming and Computer Science
Replies
4
Views
903
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
3
Views
734
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
928
Back
Top