Have a function return a complex number in C?

In summary, C does not seem to support complex numbers in a way that allows for easy return of the value. I am thinking that I would need to use a pair or a struct, with two doubles inside in both cases.
  • #1
denjay
77
0
So I wrote a program that has one main function call two other functions in an equation and then calculates a value. The problem is the numbers I need returned from those two other functions need to stay in their complex form. Using double() I get an error when trying to return that. Is there any other function type that can return the numbers in their complex form?

Note: the program is in C.
 
Technology news on Phys.org
  • #2
I think I would use a pair or a struct, with two doubles inside in both cases.

Edit: Oh, I didn't know C can handle complex numbers like that.
 
Last edited:
  • #3
I would use the complex numbers that are part of the C99 standard and declare the function as double complex.
 
  • #4
I'm still having this problem so I'll post an abridged part of my code.

I took out a lot but kept the relevant parts.

The loop acts to calculate rn[1] which is the value I need to return. My thought is that the function type is wrong or the variable type is wrong, or that C just doesn't return a complex number?

Code:
#include "cplx.h"

...

double _Complex rnw(double P[], double C[], double x)  
{
 struct cplx f[20000], an[20000], rn[20000], fnmax, f1, rn1;

...


for (n=nmax; n>=2; n--) {
      an[n-1]=cdiv( cdiff (f[n-1],f[n]),csum(f[n-1],f[n]) );
      rn[n-1]=cmult( cplxexp( cmult(makecplx(0,-2.*C[5]*d[n-1]),f[n-1])), cdiv( csum(rn[n],an[n-1]),csum(cmult(rn[n],an[n-1]),makecplx(1.0,0) )));
    }
    return rn[1];
}
 
  • #5
Where do you set the type of rn[]? Is it double _Complex?
 
  • #6
The code does not follow C99 standards at all, as far as I can see, anyway. Plus your return statement is in the middle of a for(...) loop. C99 is the point at which complex support became part of standard C.

The arrays (like rn[]) should be declared as the very same data type as the function: _Complex. I am guessing struct cplx is supposed to be the same datatype as _Complex. But I don't know.

I highly recommend that you follow C standards. Compile to show all warnings, a correct program NEVER compiles with warnings. Even if you can run a program compiled with warnings and it appears to work. This is called 'programming by accident'.
 
  • #7
jim mcnamara said:
Plus your return statement is in the middle of a for(...) loop.
It is outside, the indentation is a bit confusing.

Compile to show all warnings, a correct program NEVER compiles with warnings. Even if you can run a program compiled with warnings and it appears to work. This is called 'programming by accident'.
Well, things like 'unsigned int numberofwhatever=10; for(int i=0; i<numberofwhatever; i++)" (where the first one is hidden somewhere in an object in a completely different file) give a warning about the comparison between signed and unsigned integers (depends a bit on the compiler), but it does not 'work by accident', it just works.
I think I even saw that warning with "for(int i=0; i<10; i++)" once.
 
  • #8
for(int i=0; i<10; i++) is legal C99 - the scope of the variable i is the duration of the loop. And you are correct - the snippet confused me - the return value is in a reasonable place.
 

Related to Have a function return a complex number in C?

1. How do I create and return a complex number in C?

To create a complex number in C, you can use the complex data type. Use the creal() and cimag() functions to assign real and imaginary values respectively, and then use the complex() function to combine them into a single complex number. Finally, use the return statement to return the complex number from your function.

2. Can a function return both real and imaginary parts of a complex number separately?

Yes, a function can return both the real and imaginary parts of a complex number separately. You can use the creal() and cimag() functions to access the real and imaginary parts, respectively, of a complex number. These functions take a complex number as an argument and return a double value.

3. How do I declare a function that returns a complex number?

To declare a function that returns a complex number in C, you can use the complex data type as the return type of the function. For example, your function declaration could look like this: complex myFunction(); This tells the compiler that your function will return a complex number when called.

4. Is it possible to pass a complex number as an argument to a function and have the function return a modified complex number?

Yes, it is possible to pass a complex number as an argument to a function and have the function return a modified complex number. You can pass a complex number as an argument by using the complex data type in the function's parameter list. The function can then modify the complex number and return the modified version using the return statement.

5. Can I use the printf() function to print a complex number returned by a function?

Yes, you can use the printf() function to print a complex number returned by a function. To do this, you will need to use the %lf format specifier to print the real and imaginary parts of the complex number separately. For example, if your complex number is stored in a variable called myNum, you could print it using printf("%lf + %lfi", creal(myNum), cimag(myNum)).

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
291
  • Programming and Computer Science
Replies
2
Views
724
  • Programming and Computer Science
Replies
6
Views
981
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
11
Views
1K
Replies
3
Views
827
  • Programming and Computer Science
Replies
4
Views
766
  • Programming and Computer Science
Replies
8
Views
400
Back
Top