Question: Representing complex numbers in C

Dave P. Schaumann dave at cs.arizona.edu
Thu Feb 28 14:32:14 AEST 1991


In article <12620.27cbe36f at ecs.umass.edu> lim at 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 at 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



More information about the Comp.lang.c mailing list