Complex numbers and printf

B

Bartc

jacob navia said:
The C99 standard forgot to define the printf equivalent for complex
numbers


Since I am revising the lcc-win implementation of complex numbers
I decided to fill this hole with


printf("%#Zg\n",m);
2.00000+3.00000i

As has been mentioned, it may not be flexible enough for all situations, but
may be convenient sometimes.

But inventing new printf formats is fun; how about the following for
printing arrays and blocks of data:

int a[] = {10,20,30,40,50,60};

printf(" (%A,%d)\n",a,6);

will print: (10,20,30,40,50,60)

%A means array print, the parameter should point to the data. An extra
int/size_t parameter is how many values to print. The format of each element
follows (%d). The text between %A and %d is separator chars. Maybe %10A for
10 elems per line.

Might get more use than %Z anyway.
 
M

Martin Ambuhl

Bartc said:
But inventing new printf formats is fun; how about the following for
printing arrays and blocks of data:

int a[] = {10,20,30,40,50,60};

printf(" (%A,%d)\n",a,6);

will print: (10,20,30,40,50,60)

%A means array print,

You'll need to choose some other specifier. "%A" already is defined for
signed hexadecimal floating-point conversion.
 
E

Eric Sosman

Morris said:
Bartc said:
But inventing new printf formats is fun; how about the following for
printing arrays and blocks of data:

int a[] = {10,20,30,40,50,60};

printf(" (%A,%d)\n",a,6);

will print: (10,20,30,40,50,60)

%A means array print, the parameter should point to the data. An extra
int/size_t parameter is how many values to print. The format of each element
follows (%d). The text between %A and %d is separator chars. Maybe %10A for
10 elems per line.

Might get more use than %Z anyway.

Cool! How about:

printf("%f",a[i=0; i<6; i++]);

Ahhh, that conjures up memories, that does. It was hard
work shovelling punched cards into the computer's firebox
(steam-powered computers in those days; had to keep up the
pressure if you wanted any accuracy), but back then I was
young, and strong, and stupid in that particular way that
expressed itself in ever more ingenious combinations of WRITE
and FORMAT. Little programming languages embedded inside the
larger one, seeming at times almost Turing-complete.

The old memories fade and blur, but never quite die ...
 
B

burton.samogradHEY

Morris Dovey said:
Morris Dovey said:
printf("%f ",a[i=0; i<6; i++]);

Heh, I just had to try that thinking I missed a new feature of C99 ;-)

I wish. It was just an "implied DO loop" adapted from FORTRAN IV
(and later) READ/WRITE statements - wishful thinking on my part.

Wow, that's an interesting syntactic construct for a complied
language. I haven't looked at FORTRAN at all, but that's pretty
interesting that they have that feature. I was looking closer at the
C version you wrote and was wondering if it even could be implemented
in your typical C compiler...

/me goes off to read more about FORTRAN...
 
W

Walter Roberson

Walter Roberson wrote:
Operator overloading in printf?

Why -not- operator overloading in printf() ? Other than, of course,
the matter of whether printf() qualifies as an "operator"
or not.

Urrr, you do allow operator overloading of varadic functions,
don't you?

Funny how much nonsense you can say. Are you paid by the nonsense line?
Or you do it for free as a hobby?

Why, the union contract sets out a monthly quota of course.
 
S

Serve Laurijssen

Walter Roberson said:
Then there is engineering, in which the imaginary constant is
usually represented as 'j' instead of 'i' or 'I'.

I guess that is why it isnt put in and it wasnt an oversight.
It is interesting to see that in C++ you can output complex numbers with
cout, although I dont know if thats standard.

std::complex<double> c(5., 6.);
std::cout << c << std::endl;

this prints: "(5,6)" on my machine

If thats standard I would say use that same mechanism in lccwin32 too and
also in the C standard if it ever gets adopted. More consistent this way
 
N

Noob

"We're sorry. You have reached an imaginary number. Please rotate
your phone 90 degrees and try again."
 
D

David Thompson

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
 
G

Gerry Ford

David Thompson said:
One of many possible representations.

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

Nuts, Sr. Thompson.

I usually read your posts as a bellweather to syntax.

Fortran and printf: don't do it. Pass it instead.

--
Gerry Ford



"Anybody who says, that a high-speed collision with water is the same as
with concrete, likely has more experience with the former than the latter."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top