1

Im trying to create a complex numbers library in C by using a typedef of an structure.

typedef struct{
     double real;
     double imag;
}complex_t;

However I'm curious whether if it's plausible to use this definition to do some computations using the '+' and '*' sign to operate complex numbers, instead of creating a complex_t sum(complex_t z,complex_t w) or complex_t prod(complex_t z,complex_t w) . I want to do something like this :

#include<complex_library.h>

complex_t z1,z_2,w_1,w_2;
z_1 = newComplex(1,2);
z_2 = newComplex(-3,-4);

w_1 = z_1 + z_2;
w_2 = z_1 * z_2;

Thanks.

  • 1
    C is not C++. You can not overload operators – Amadeus Jul 12 '19 at 20:37
  • 9
    C has built in complex type. No need to roll your own. – n. m. could be an AI Jul 12 '19 at 20:38
  • 2
    Why can't you use the already built in support for complex numbers in C99, see e.g. https://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c ? – CuriouslyRecurringThoughts Jul 12 '19 at 20:38
  • 1
    @CuriouslyRecurringThoughts That would be my choice, though it is worth noting that `_Complex` is an optional feature. – Christian Gibbons Jul 12 '19 at 20:42
  • You'll need `static inline complex_t addComplex(complex_t z1, complex_t z) { return (complex_t){ .real = z1.real + z2.real, .imag = z1.imag + z.imag}; }` and you will then call `z3 = addComplex(z1, z2);` — etc. In other words, you'll have to write your own function to do the arithmetic, or use the built-in complex arithmetic instead. The latter is less effort on your part, if your compiler supports it. And some of the operator functions are much more complicated (dare one say 'complex') than addition. – Jonathan Leffler Jul 12 '19 at 20:43
  • @ChristianGibbons fair point, I was unaware that they were optional. Thank you for pointing it out. – CuriouslyRecurringThoughts Jul 12 '19 at 20:44
  • @ChristianGibbons Almost any feature of C is potentially optional. A C compiler could exist without `union`; then it won't process code that uses unions. We would call the compiler non-conforming; yet it has parts that do conform and accepts many programs. Same thing with a compiler that doesn't have `_Complex`: it doesn't accept some programs that use a certain standard feature, but accepts others. For no reason other than how the standard is worded, the implementation without `_Complex` can be called "conforming". It's just a word game that doesn't change what works and doesn't. – Kaz Jul 12 '19 at 21:04
  • 2
    @Kaz The distinction between "standard conformant C compiler" and "compiler that doesn't follow the standard" is pretty big to me. If I can't trust the C standard documentation to be accurate when using a particular "C" compiler then the compiler is garbage. If one intends to write portable code, it is important to know the distinction between required language features and optional features, as well as what is implementation-defined behavior. A compiler that doesn't support the required `union` feature is garbage. Not supporting the, for example, optional Annex K, however, is rather common. – Christian Gibbons Jul 12 '19 at 21:36
  • @ChristianGibbons Someone who finds that `_Complex` is missing might also regard the compiler as garbage for their purposes, and have to find a non-garbage compiler. Also note that C99 introduced certain required features which **later** become optional, due to numerous implementations not bothering to implement them. Optional is just a word which *papers* over the fact that "we made a hard requirement nobody cares about", without changing reality in any way. – Kaz Jul 12 '19 at 22:43
  • @Kaz If someone needs `_Complex`, and the compiler doesn't support it, then it is true that it will not work for them, unless they implement their own complex functions or use another library. But it is well documented by the standard to be optional so a developer would know to check the `__STDC_NO_COMPLEX__` macro. And no, "optional" is not a "hard requirement nobody cares about". `intN_t` types, for example, are optional, but they are hardly something nobody cares about. Optional means exactly what it denotes: the compiler may optionally support the standardized feature. – Christian Gibbons Jul 12 '19 at 22:57
  • Kaz: now you're spreading FUD. The most significant were VLAs that weren't unimplemented by numerous implementations, no, it was Microsoft and their pitiful excuse of a "C" compiler. – Antti Haapala -- Слава Україні Jul 13 '19 at 00:21
  • @Kaz You cannot compare optional with non-optional features. If I write a standard conforming program, then I can trust that it will compile and behave the same way irregardless of what compiler I use. I can also trust that it will do the same in the future. Sure, some exceptions does exist, like the function `gets` but they are rare. And besides, adding VLA:s was a huge mistake anyway. I'm glad they removed it as a requirement. – klutt Oct 28 '19 at 22:46

2 Answers2

1

I'm almost ashamed to post this as an answer, but it is a valid question and every question deserves an answer.

I'm curious whether if it's plausible to use this definition to do some computations using the '+' and '*'

No, it is not possible to do. C simply does not support operator overloading. C++ supports this, but not C.

Sidenote: Avoid using the _t suffix. It's reserved in POSIX. Sure, it's not reserved in the C standard but POSIX is pretty big.

Names that end with ‘_t’ are reserved for additional type names.

https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html

klutt
  • 30,332
  • 17
  • 55
  • 95
0

For evaluating +,-,*,/ operations on complex no.s, better to define functions that operates on real and imaginary parts separately.Since operator overloading is not available in C, defining separate functions seems to be the only option.Hope that helps.

chinmay_manas
  • 86
  • 1
  • 4