jacob navia <
[email protected]> writes:
Second, it's inflexible; it imposes one of two output representations
One of many possible 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).
More specifically _list-directed_ output does that, and is standard
since at least F77 (see below). For small values such as your example;
for values outside an implementation-dependent range it will switch to
E-style instead of F-style, much like Fortran G or C printf %g
switches for a real (noncomplex) float. And yes, that list-directed
(and namelist) output and input syntax is I believe deliberately the
same as for a literal in source, much as C scanf %i or strtol(,,0)
accepts 0octal and 0xhex (plus sign). <OT>(And inetaddr!)</>
I _think_ list (and namelist) was new in F77, but since at least
FIV=66 Fortran has had explicitly-formatted output and input of
COMPLEX treated as a series of two REALs, in the same way it treats an
array (or in F90+ an array section) as its elements in order. (And
F90+ treats a struct-like TYPE as its components in order, unless in
F03 you provide a user-defined routine.) Fortran does and did allow
any data format descriptor (or parenthesized part of a format) to have
a repeat count, so a single descriptor like 2F8.3 can handle both
parts of a COMPLEX if you want NO punctuation, in the same way say
99I5 can handle an array of 99 (or less) INTEGERs.
This is more like the C99 approach except for the convenience of not
writing out 'realpart' and 'imagpart' selectors. Or rather, we could
say C99 printf follows the practice (long) established by Fortran as
closely as its function-call-by-value (vs statement) easily allows.
- formerly david.thompson1 || achar(64) || worldnet.att.net