Complex numbers and printf

J

jacob navia

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
"Z"

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
 
G

Gerry Ford

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
"Z"

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 don't think it's quite like that, Jacob, much like the dinner I ingested
on the Champs d'Elyzee. Unlike those 6 chefs 2 dieners, who knew to feed my
wife excellent cuisine while they fed me a horse's willy that I either gave
to my shirt pocket or to the sidewalk on our way back to the hotel, I
actually expected a meal.

I'd be interested to try a new version of lcc.
 
D

Doug Miller

I don't think it's quite like that, Jacob, much like the dinner I ingested
on the Champs d'Elyzee. Unlike those 6 chefs 2 dieners, who knew to feed my
wife excellent cuisine while they fed me a horse's willy that I either gave
to my shirt pocket or to the sidewalk on our way back to the hotel, I
actually expected a meal.

In future, please sober up before posting. Thank you.
 
J

jacob navia

Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?
 
J

Joachim Schmitz

jacob said:
Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?
Would it give a diagnostic if called in conforming mode?

Bye, Jojo
 
J

Joachim Schmitz

jacob said:
Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?
You initial post sounded quite matter-of-factly and not like e request for
comments...
 
J

jacob navia

Joachim said:
jacob said:
Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?
Would it give a diagnostic if called in conforming mode?

Bye, Jojo

No.

I do not think it is necessary since this is an
illegal combination for printf anyway. It WILL give
a warning if you do not pass it a complex number
of course.
 
J

jacob navia

Joachim said:
jacob said:
Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?
You initial post sounded quite matter-of-factly and not like e request for
comments...

Excuse me. I should have request comments more explicitely.

I hope I do not mess something else. I used "Z" since in the lasts
discussions (about my "b" extension) people complained that it was a
lower case letter. Then I used now upper case ones.
 
R

Richard Heathfield

Joachim Schmitz said:
jacob said:
Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?
Would it give a diagnostic if called in conforming mode?

No diagnostic message is required for invalid format specifiers to printf.
The behaviour is undefined, and implementations can do anything they like
as a consequence. In lcc-win32, maybe the implementation will treat the
argument as a complex number. Perhaps under some other implementation
it'll format the hard disk. Life gets exciting when you allow your code to
rely on non-standard extensions and then re-compile the code in a
different environment.

I'm not quite sure why this thread is cross-posted to comp.lang.c - it
seems to be an entirely lcc-related subject.
 
C

CBFalconer

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

I don't believe so. Formatting of such dumps is easily done by
selecting the real and imaginary portions, and using the existing
printf operations to format them. Why standardize something that
will not be used and is unnecessary?
 
N

Nelu

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
"Z"

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

Is it worth doing this? I mean is there a good reason to make the
programmer remember an extra formatting option that's non standard
instead of allowing him to use what he already knows? I mean doing printf
("%f+%f*I\n",real(m),imag(m)) is not that big of an effort, is it?
However, if you really want to have this formatting option it's probably
a good idea to talk to the guys in comp.std.c and see if they are
considering something like that and whether or not your implementation
may be affected by the standard in the future.
 
L

lawrence.jones

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

No it didn't. The committee made a concious decision *not* to define a
format for complex numbers since there is no agreed upon format and it's
easy enough to format the real and imaginary parts yourself however you
desire.

-Larry Jones

I take it there's no qualifying exam to be a Dad. -- Calvin
 
K

Keith Thompson

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."
 
W

Walter Roberson

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
"Z"

Historically, Z was usually associated with hexadecimal
(but it has been years since I've seen that.)
 
W

Walter Roberson

Keith Thompson said:
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.

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

Nelu

On Fri, 29 Feb 2008 16:08:59 +0000, Nelu wrote:


printf ("%f+%f*I\n",real(m),imag(m)) is not that big of an effort, is

That's probably creal and cimag. I think the real/imag forms are only
available in C++.
 
M

Morris Dovey

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++]);
 
W

Walter Roberson

jacob navia wrote:
I don't believe so. Formatting of such dumps is easily done by
selecting the real and imaginary portions, and using the existing
printf operations to format them. Why standardize something that
will not be used and is unnecessary?

Because it gives an opportunity to demonstrate the incomparible
beauty of operator overloading??
 
J

jacob navia

Walter said:
Because it gives an opportunity to demonstrate the incomparible
beauty of operator overloading??

Operator overloading in printf?

Funny how much nonsense you can say. Are you paid by the nonsense line?

Or you do it for free as a hobby?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top