jacob navia said:
The C99 standard forgot to define the printf equivalent for complex numbers
I doubt that they forgot; they probably just didn't feel it was
necessary. In my opinion, they were right.
Since I am revising the lcc-win implementation of complex numbers
I decided to fill this hole with
"Z"
Do you have a similar extension for scanf?
for instance
double _Complex m = 2+3*I;
printf("%Zg\n",m);
will print
2+3*I
The alternative flag makes this look like:
printf("%#Zg\n",m);
2.00000+3.00000i
I see no problem with this extension in terms of conformance to the
standard. Though I don't see any particular need for a special format
for complex numbers, I wouldn't object if such a format were added to
a future version of the standard.
However, if I were a user of lcc-win, I probably wouldn't use it in my
own code, for several reasons.
First, it is of course an extension. Unless my code already depends
on other lcc-win extensions, I'd rather not limit its portability for
the sake of a minor convenience.
Second, it's inflexible; it imposes one of two output representations
for complex numbers. If I want to print "2+3*I", I can write:
printf("%g+%g*I\n", creal(m), cimag(m));
If I want to print "2 + 3i", of course, I can write:
printf("%g + %gi\n", creal(m), cimag(m));
which your extension doesn't support, but the standard already does.
In effect, I tend to think of a complex number as a number (usually)
for purposes of arithmetic, but as a composite structure for purposes
of I/O. Perhaps that's just me.
Finally Fortran has supported complex numbers, and I/O on them, since
dinosaurs walked the Earth. The result of printing 2+3*I in Fortran
would be "(2.,3.)" (at least by default using g77; I don't know
Fortran well enough to know whether that's standard). That form is
also valid in Fortran source as a complex literal (insert previous
disclaimer here).
Note that a parenthesized format might make things easier for a
corresponding scanf extension.
This is not to say that your extension is a bad idea, merely that I
personally don't find it to be a sufficiently good idea to pique my
interest. (And yet here I am writing about it at some length.)
Or, as "Gerry Ford" might put it, "Bertha termite prefecture
psychopomp inhabit dovetail statuette virus superlative deposition."