Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tilde '~' operator on _Complex, what does it do? Is it an extension?

Tags:

c

c99

complex.h

In C99, it looks like the '~' operator on a _Complex performs a complex conjugate. The following code:

#include <complex.h>
#include <stdio.h>
int main()
{
    double _Complex a = 2 + 3 * I;
    printf("%f,%f\n", creal(~a), cimag(~a));
}

Gives the output:

2.000000,-3.000000

This behaves the same in both gcc and clang. Is this an extension? I can't seem to find any reference to it in the various standards documents google pulled up.

If it is an extension, is there a way to deactivate it?

like image 286
A Q Avatar asked Oct 16 '25 20:10

A Q


1 Answers

This is in fact a gcc extension, documented in section 6.11 of the gcc manual:

The operator ~ performs complex conjugation when used on a value with a complex type. This is a GNU extension; for values of floating type, you should use the ISO C99 functions conjf, conj and conjl, declared in <complex.h> and also provided as built-in functions by GCC.

If you compile with the -pedantic flag, you'll get a warning for this:

x1.c: In function ‘main’:
x1.c:6:29: warning: ISO C does not support ‘~’ for complex conjugation [-Wpedantic]
     printf("%f,%f\n", creal(~a), cimag(~a));
                             ^
x1.c:6:40: warning: ISO C does not support ‘~’ for complex conjugation [-Wpedantic]
     printf("%f,%f\n", creal(~a), cimag(~a));
like image 61
dbush Avatar answered Oct 18 '25 09:10

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!