Have a function return a complex number in C?

Click For Summary
SUMMARY

The discussion centers on returning complex numbers in C using the C99 standard. The user initially attempted to return complex values using a double type, which resulted in errors. It was clarified that C supports complex numbers through the double _Complex type, and that arrays such as rn[] should also be declared as double _Complex to maintain consistency. Additionally, the importance of adhering to C standards and compiling without warnings was emphasized to avoid programming errors.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with C99 standard and its complex number support
  • Knowledge of data types, specifically double _Complex
  • Experience with compiling C code and interpreting compiler warnings
NEXT STEPS
  • Learn about the complex.h header in C and its functions
  • Research best practices for declaring and using complex numbers in C
  • Explore debugging techniques for resolving compiler warnings in C
  • Study examples of complex number operations in C using double _Complex
USEFUL FOR

C developers, software engineers working with numerical computations, and anyone interested in implementing complex number arithmetic in C programming.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
6
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
20
Views
2K
Replies
3
Views
1K