Have a function return a complex number in C?

AI Thread Summary
The discussion revolves around a C program that calculates a value using complex numbers, specifically addressing issues with returning complex values from functions. The original poster initially attempted to use the `double()` function but encountered errors when trying to maintain the complex form of the numbers. They later discovered that C99 supports complex numbers and considered using `double complex` as the return type. Key points include the need to declare arrays like `rn[]` with the same data type as the function's return type, which should be `_Complex`. There is a consensus that the code does not adhere to C99 standards, particularly regarding complex number handling. The importance of compiling code without warnings is emphasized, as warnings can indicate potential issues that may lead to unpredictable behavior. The return statement's placement within the loop was clarified, with the consensus that it is correctly positioned outside the loop despite initial confusion. Overall, the discussion highlights the necessity of following C standards for proper complex number manipulation and the importance of clean, warning-free code.
denjay
Messages
77
Reaction score
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
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:
I would use the complex numbers that are part of the C99 standard and declare the function as double complex.
 
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];
}
 
Where do you set the type of rn[]? Is it double _Complex?
 
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'.
 
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.
 
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.
 
Back
Top