473,383 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Complex numbers and printf

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

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Feb 29 '08 #1
25 9611

"jacob navia" <ja***@nospam.comwrote in message
news:fq**********@aioe.org...
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.

--
Gerry Ford

"Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
beziehend.
Feb 29 '08 #2
In article <12**************@news.newsgroups.com>, "Gerry Ford" <in*****@invalid.netwrote:
>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.
Feb 29 '08 #3
Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Feb 29 '08 #4
jacob navia wrote:
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
Feb 29 '08 #5
jacob navia wrote:
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...
Feb 29 '08 #6
Joachim Schmitz wrote:
jacob navia wrote:
>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.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Feb 29 '08 #7
Joachim Schmitz said:
jacob navia wrote:
>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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 29 '08 #8
On Fri, 29 Feb 2008 15:50:17 +0100, jacob navia wrote:
Gerry Ford wrote:

[bad dinner left a lasting impression]

Coming back to complex numbers... Any comments?
What about complex numbers?
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 29 '08 #9
On Fri, 29 Feb 2008 10:34:17 +0100, jacob navia wrote:
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.


--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 29 '08 #10
jacob navia <ja***@nospam.comwrites:
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."

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 29 '08 #11
jacob navia wrote:
>
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?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 29 '08 #12
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
>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'.
--
"Style is instinctive and few achieve it in a notable degree. Its
development is not hastened by instruction. It comes or it doesn't.
It will take care of itself." -- Walter J. Phillips
Feb 29 '08 #13
On Fri, 29 Feb 2008 16:08:59 +0000, Nelu wrote:
<snip>
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++.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 29 '08 #14
In article <47***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>jacob navia wrote:
>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?
Because it gives an opportunity to demonstrate the incomparible
beauty of operator overloading??
--
"To all, to each! a fair good-night,
And pleasing dreams, and slumbers light" -- Sir Walter Scott
Feb 29 '08 #15
Walter Roberson wrote:
In article <47***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>jacob navia wrote:
>>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?

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?
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Feb 29 '08 #16

"jacob navia" <ja***@nospam.comwrote in message
news:fq**********@aioe.org...
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+3*I
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.

--
Bart
Feb 29 '08 #17
Bartc wrote:
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++]);

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/Stirling/
Feb 29 '08 #18
Bartc wrote:
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.
Feb 29 '08 #19
Morris Dovey <mr*****@iedu.comwrites:
Bartc wrote:
Cool! How about:

printf("%f",a[i=0; i<6; i++]);
Heh, I just had to try that thinking I missed a new feature of C99 ;-)

--
burton
Feb 29 '08 #20
Morris Dovey wrote:
Bartc wrote:
>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 ...

--
Er*********@sun.com

Feb 29 '08 #21
bu*****************@AM.gmail.com wrote:
>
Morris Dovey <mr*****@iedu.comwrites:
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.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/Stirling/
Feb 29 '08 #22
Morris Dovey <mr*****@iedu.comwrites:
bu*****************@AM.gmail.com wrote:
>>
Morris Dovey <mr*****@iedu.comwrites:
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...

--
burton
Feb 29 '08 #23
In article <fq**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:
>Walter Roberson wrote:
>Because it gives an opportunity to demonstrate the incomparible
beauty of operator overloading??
>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.

--
"Prevention is the daughter of intelligence."
-- Sir Walter Raleigh
Mar 1 '08 #24

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.caschreef in bericht
news:fq**********@canopus.cc.umanitoba.ca...
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
>>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'.
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<doublec(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
--
"Style is instinctive and few achieve it in a notable degree. Its
development is not hastened by instruction. It comes or it doesn't.
It will take care of itself." -- Walter J. Phillips
Mar 1 '08 #25
"We're sorry. You have reached an imaginary number. Please rotate
your phone 90 degrees and try again."
Mar 3 '08 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am...
6
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
4
by: Mantorok Redgormor | last post by:
With what specifier do I use to print a variable of a complex type? The man page for printf doesn't even say. - nethlek
7
by: Don Tucker | last post by:
Hello, I have the following test program that I can compile with gcc, but when I try to compile with the portland group compiler, pgcc, I get a stream of errors. #include <stdio.h> #include...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
27
by: David Marsh | last post by:
I understand that C99 supports a complex type and complex arithmetic. There is nothing about it in the FAQ and online searches turned up very little except synopses. Can anyone point me toward...
11
by: jacob navia | last post by:
hi I am trying to use the complex data type. Consider this code, taken from the cclib library: struct complex csqrt(Cpx z) { double r; r=sqrt(z.re*z.re+z.im*z.im);...
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
4
by: Reshmi | last post by:
Hi, I have this C code which does complex number arithmetic. When I try to write a similar file for C++, it says that "creal’ was not declared in this scope". Can anyone give a better idea to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.