How to programing with complex number in cprograming.

xinzlee

Joined Nov 19, 2007
13
I face the problem when want programing by using sum, dived, multi, minus in complex number... so how to do it.

Dave

Joined Nov 17, 2003
6,970
Do you have a programming language that you are thinking of? If you use something like Matlab there is syntax dedicate to complex numbers, otherwise it is no too difficult to understand the basic concepts of +-*/ for complex numbers.

Dave

Papabravo

Joined Feb 24, 2006
16,938
In C the easiest way to do what you want is to use a structure with two members representing the real part and the imaginary part.

Rich (BB code):
typedef struct complex_t
{
float r ;
float i ;
} COMPLEX ;

static COMPLEX    r1 ;

{
r1.r = a->r + b->r ;     /* Add real parts  */
r1.i = a->i + b->i ;     /* Add imaginary parts   */
return &r1 ;             /* Return pointer to structure  */
}

COMPLEX *pr1 ;
COMPLEX c1, c2 ;
void main(void)
{
c1.r = 2.1 ; c1.i = 4.3 ;
c2.r = 1.6 ; c2.i = 3.2 ;

/*
**    pr1->r should be 3.7
**    pr1->i should be 7.5
*/
}
The reason for using pointers is that many compilers will not allow the passing of arrays and structures by value which is the common convention in C compilers. The the value passed needs to be a pointer to the object.