2012-08-10 17:15:25 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2019-01-11 23:04:34 +08:00
|
|
|
#ifdef __GNUC__
|
2014-06-16 17:41:29 +08:00
|
|
|
_Complex c; // this is usually '_Complex double'
|
2012-08-10 17:15:25 +08:00
|
|
|
c=1.0i+2;
|
|
|
|
|
2012-09-11 00:14:15 +08:00
|
|
|
assert(__real__ c == 2);
|
2016-11-28 23:41:24 +08:00
|
|
|
assert(__imag__ c == 1);
|
|
|
|
|
2012-08-10 17:15:25 +08:00
|
|
|
_Complex double d;
|
2012-09-11 00:14:15 +08:00
|
|
|
assert(sizeof(c)==sizeof(d));
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2013-02-12 18:20:52 +08:00
|
|
|
_Complex signed char char_complex, char_complex2;
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2012-08-10 17:57:52 +08:00
|
|
|
char_complex=0x3i-2;
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2012-08-10 17:15:25 +08:00
|
|
|
assert(sizeof(d)==sizeof(c));
|
2013-02-12 18:20:52 +08:00
|
|
|
assert(sizeof(char_complex)==sizeof(signed char)*2);
|
2012-09-11 00:14:15 +08:00
|
|
|
|
2013-02-12 02:21:45 +08:00
|
|
|
// The real part is stored first in memory on i386.
|
|
|
|
// Need to find out about other architectures.
|
2019-01-11 23:04:34 +08:00
|
|
|
#if defined(__i386__) || defined(__amd64__)
|
2013-02-12 18:20:52 +08:00
|
|
|
assert(((signed char *)&char_complex)[0]==-2);
|
|
|
|
assert(((signed char *)&char_complex)[1]==3);
|
2019-01-11 23:04:34 +08:00
|
|
|
#endif
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2012-08-10 17:15:25 +08:00
|
|
|
assert(__real__ char_complex == -2);
|
|
|
|
assert(__imag__ char_complex == 3);
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2014-06-16 17:41:29 +08:00
|
|
|
// the precedence of __imag__ is higher than that of +
|
|
|
|
assert((__imag__ 1.0i + 1.0i) == 1.0i + 1.0);
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2012-08-10 17:15:25 +08:00
|
|
|
// complex conjugate
|
|
|
|
char_complex2 = ~ char_complex;
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2012-09-14 19:33:02 +08:00
|
|
|
// __real__ something is an lvalue!
|
2016-11-28 23:41:24 +08:00
|
|
|
__real__ char_complex = 100;
|
2012-09-14 19:33:02 +08:00
|
|
|
assert(__real__ char_complex == 100);
|
|
|
|
assert(__imag__ char_complex == 3);
|
|
|
|
|
2016-11-28 23:41:24 +08:00
|
|
|
// can be incremented
|
2012-09-14 19:33:02 +08:00
|
|
|
char_complex++;
|
|
|
|
assert(__real__ char_complex == 101);
|
|
|
|
assert(__imag__ char_complex == 3);
|
2019-01-11 23:04:34 +08:00
|
|
|
++char_complex;
|
|
|
|
assert(__real__ char_complex == 102);
|
|
|
|
assert(__imag__ char_complex == 3);
|
|
|
|
char_complex += 1;
|
|
|
|
assert(__real__ char_complex == 103);
|
|
|
|
assert(__imag__ char_complex == 3);
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2012-09-14 19:33:02 +08:00
|
|
|
// also separately
|
|
|
|
(__real__ char_complex)++;
|
2019-01-11 23:04:34 +08:00
|
|
|
assert(__real__ char_complex == 104);
|
2012-09-14 19:33:02 +08:00
|
|
|
assert(__imag__ char_complex == 3);
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2012-09-22 20:08:12 +08:00
|
|
|
// casts to reals produce the real part
|
2019-01-11 23:04:34 +08:00
|
|
|
assert((int)char_complex == 104);
|
|
|
|
|
|
|
|
// can be decremented
|
|
|
|
char_complex--;
|
|
|
|
assert(__real__ char_complex == 103);
|
|
|
|
assert(__imag__ char_complex == 3);
|
|
|
|
--char_complex;
|
|
|
|
assert(__real__ char_complex == 102);
|
|
|
|
assert(__imag__ char_complex == 3);
|
|
|
|
char_complex -= 1;
|
|
|
|
assert(__real__ char_complex == 101);
|
|
|
|
assert(__imag__ char_complex == 3);
|
2016-11-28 23:41:24 +08:00
|
|
|
|
2019-01-11 23:04:34 +08:00
|
|
|
#else
|
2016-11-28 23:41:24 +08:00
|
|
|
|
|
|
|
// Visual studio doesn't have it
|
2012-08-12 01:24:35 +08:00
|
|
|
|
2019-01-11 23:04:34 +08:00
|
|
|
#endif
|
2012-08-10 17:15:25 +08:00
|
|
|
}
|