How are complex arithmetic operators implemented in C?

688 views Asked by At

The ANSI C standard doesn't seem to explicitly specify any details on the API for complex arithmetic operators (+,-,*,/), though it seems that the implementation allows use of these primitives. I'm wondering how this is possible, since C does not implement the concept of operator overloading? My thought is that an implementation of the macros specifying the complex type will most likely utilize a built-in type which does permit such operations, but I then wonder how a complex number could permit the same range of values as, e.g., a single floating-point number (since it would have to somehow be represented as a pair of these, I would think its range would be cut in half)?

Am I thinking along the right lines, or am I way off on this one?

2

There are 2 answers

1
Chris Dodd On BEST ANSWER

C does have operator overloading -- even before the C89 standard, it overloaded operators like +, -, *, and / for both integer and floating point operations.

C does not have user defined operator overloading -- so there are just the fixed set of overloads defined in the standard and no way to extend them.

C99 just extends the int/float overloading of operators to complex numbers. There's still no way for a program to extend the overloading beyond that specified in the spec.

4
Jens Gustedt On

Since C99 complex types are an optional part of the C language, including support for all arithmetic operation.

And to answer your question about the representation, they are defined as pairs of a real and an imaginary part, just as one would expect.

Your reference to something like "ANSI C" is not well defined, because all ISO C standards usually get adopted by ANSI, too. So it is easier to refer to the different versions of C through their publication year, C89, C99 and C11.