Have a function return a complex number in C?

Click For Summary

Discussion Overview

The discussion revolves around returning complex numbers from functions in C, specifically addressing issues related to data types and adherence to the C99 standard. Participants explore various methods to handle complex numbers in their code, including the use of structs and the built-in complex number support in C99.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in returning complex numbers from functions and questions the appropriate function type to use.
  • Another participant suggests using a pair or struct with two doubles to represent complex numbers.
  • A different participant recommends using the complex numbers defined in the C99 standard, proposing to declare the function as double complex.
  • A participant shares a code snippet and expresses uncertainty about the variable types and function return types related to complex numbers.
  • One participant questions how the type of the rn[] array is set, asking if it should be double _Complex.
  • Another participant critiques the code for not following C99 standards and points out that the return statement is incorrectly placed within a loop, although later clarifications indicate it is actually outside the loop.
  • Concerns are raised about compiling code with warnings, emphasizing that a correct program should not compile with warnings.
  • There is a discussion about the legality of variable scope in C99, with one participant clarifying that declaring a variable within a for loop is valid.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to handle complex numbers in C, with no consensus reached on a single solution. Some participants advocate for using C99's built-in complex number support, while others suggest alternative methods like structs.

Contextual Notes

There are unresolved questions regarding the correct implementation of complex number handling in C, including potential issues with data types and adherence to standards. The discussion highlights the importance of compiling code without warnings and following established programming practices.

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