JTC1/SC22/WG14
N818
WG14/N818 J11/98-017
Creating complex numbers.
How to build a complex number in C9X (or what happens if an
implementation supports NaNs and has I be of type float
complex)?
Note: in the following, treat <i> as an italic i, meaning
the mathematical i for the sqrt(-1).
Currently, the draft suggests (page 257, footnote 188) that
a complex number z should compare equal to creal(z) +
cimag(z)*I; but this is not the same as saying a complex
number can be built up from two reals, say x and y, by doing
the expression: (x+y*I). For example, if I is defined to be
of type complex (instead of type imaginary) and y is a NaN,
(x+y*I) need not be the same as x+y*<i>. If I is of type
complex (instead of imaginary), there is a problem. The
problem is: I*NaN is NaN+NaN*<i>, instead of the expected
0+NaN*<i>, so, the sum x+I*y ends up being NaN+NaN*<i>,
instead of the expected x+NaN*<i>.
The draft states (6.1.2.5 Types, page 40) that the first
element of complex is the real part and that the second
element is the imaginary part and that complex have the
alignment and representation as an array of two real
elements.
Some proposed solutions:
1) Allow access to the components of a complex variable (as
if it were a structure with members re and im). That is,
allow one to write:
z.re = x;
z.im = y;
2) Allow access to the components of a complex variable (as
if it were an array of two elements). That is, allow one to
write:
z[0] = x; or maybe z.[0] = x;
z[1] = y; z.[1] = y;
3) Create a macro of the form:
z = CMPLX( x, y );
to create a complex number from two real numbers and where
it is understood that the first argument to the macro is the
real component and the second argument is the imaginary
component.
4) Define, by fiat, I*y to treat the real part as if it were
(negative) zero, even if I is of type complex. We believe
that this only matters if y is a NaN. The negative zero is
required if directed rounding is supported and is the value
required to have -0 + -0 be -0 in any rounding mode in IEC
559.
5) Allow compound literals of the form:
z = (double complex){ x, y };
where it is understood that the first element of the
compound literal is the real component and the second
element is the imaginary component. The current C9X draft
may already allow this in section (6.2.3.5 Compound
Literals, pages 76-79).
6) Allow in initialization lists, pairs of numbers:
double complex z = {x, y};
to be used, as if a two element array (or a structure with
two elements) were being initialized, with the first number
being the real and the second number being the imaginary.
7) Do nothing.
Any others?