[comp.lang.c] Question: Representing complex numbers in C

lim@ecs.umass.edu (02/28/91)

Hi,

I'd appreciated it if anyone out there can help me out with this problem. It 
concerns representation of complex numbers in C. I understand that Fortran has 
a data type COMPLEX that allows complex variables to be treated like any int, 
float, or double. I need to do the following in C:

COMPLEX x, y, z;
z = x + y;
etc.

The following won't work, obviously...

struct
{
 double real;
 double imaginary;
}
COMPLEX;

Thanks in advance.


Jonathan Lim

dave@cs.arizona.edu (Dave P. Schaumann) (02/28/91)

In article <12620.27cbe36f@ecs.umass.edu> lim@ecs.umass.edu writes:
>[FORTRAN can do this:]
>
>COMPLEX x, y, z;
>z = x + y;
>etc.
>
>[How is it done in C?]

Can't be done.  The closest you can come is

typedef struct { double r ; doube i } complex ;

complex x, y, z ; ...
z.r = x.r + y.r ; z.i = x.i + y.i ;

Now, you could make it a little better by defining functions to do addition,
and other ops, but that's as close as you can get in C.

C++ on the other hand, will let you overload the + operator with new
operations, just as you want.


-- 
		Dave Schaumann		dave@cs.arizona.edu
'Dog Gang'!  Where do they get off calling us the 'Dog Gang'?  I'm beginning to
think the party's over.  I'm beginning to think maybe we don't need a dog.  Or
maybe we need a *new* dog.  Or maybe we need a *cat*! - Amazing Stories

gwyn@smoke.brl.mil (Doug Gwyn) (03/01/91)

In article <12620.27cbe36f@ecs.umass.edu> lim@ecs.umass.edu writes:
>I need to do the following in C:
>COMPLEX x, y, z;
>z = x + y;

You better not "need" to do this in C, because it's impossible.
You can do something like it in C++, or you can use C in a different way.

grover@dawkins.cs.unlv.edu (Kevin Grover) (03/02/91)

In article <12620.27cbe36f@ecs.umass.edu>, lim@ecs.umass.edu writes:
) I'd appreciated it if anyone out there can help me out with this problem. It 
) concerns representation of complex numbers in C. I understand that Fortran has 
) a data type COMPLEX that allows complex variables to be treated like any int, 
) float, or double. I need to do the following in C:
) 
) COMPLEX x, y, z;
) z = x + y;
) etc.
) 
) The following won't work, obviously...
) 
) struct
) {
)  double real;
)  double imaginary;
) }
) COMPLEX;
) 
) Thanks in advance.
) 
) 
) Jonathan Lim

Well, as you've seen from the other messages, there is no easy way to do this 
in C.  However,  I ran into this problem also while trying to do some Numerical
Analysis homework.  Rather than using FORTrash, I wrote a complex number library
for C.  Even though C++ makes this library obsolete, I still have it.  If there
is some interest I could send it out.  I am not very familar with the practices
of sending code out on the net.  Where should I put the code?  On NEWS or an
FTP site?  It is many C files and some batch files to compile them into a 
library (for the various memory models.)

It supports quite a few functions:  log, sin, cos, tan, sinh, cosh, tanh
				    add, sub, mul, log, exp, (and more)

Some of the routines will also take multiple arguments.

I sort of assumed the someone else would have already written something like this
since it is not very difficult, but I didn'y have time to look and ask around.

Let me know if anyone would like this code, and tell me where and how to post it.
(it is written for Turbo C 2.0, but should work on any compiler.)

-- 
  +-------------------------------------------------+----------------------+
  | Kevin Grover             UNLV Computer Science  |     Home of the      |
  | grover@cs.unlv.edu       Las Vegas, Nevada      |    Running REBELS    |
  +-------------------------------------------------+----------------------+