473,796 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.00000 i

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Feb 29 '08
25 9730
jacob navia <ja***@nospam.c omwrites:
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.00000 i
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_Keit h) <ks***@mib.or g>
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.or g>,
Keith Thompson <ks***@mib.orgw rote:
>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",re al(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********@mai neline.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********@mai neline.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.c omwrote 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.00000 i
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.c omwrites:
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

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

Similar topics

17
3169
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 able to CRASH the backend, there is a bug here somewhere... test=# select version(); version
6
2261
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
4
10766
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
4141
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 <complex.h> int main(int argc, char **argv){ complex double a={2.3+1.4*_Complex_I}; printf("a=%lf + i%lf\n",creal(a),cimag(a));
3
2065
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 instance for "x" above, I'd like to make the format string apply to an element or elements of the instance. Can I somehow overload the "%" operator for that? Thanks.
27
2592
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 sources or give examples which show how to: -declare a complex variable -assign the real and imaginary parts -perform the basic +,-,*,/ operations on complex numbers Thanks,
11
2430
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); r=sqrt(ldexp(r+fabs(z.re),-1));
9
9952
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 = 1.0f; --------
4
4592
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 write it using "complex.h", other than using own data structures? #include <stdio.h> #include <complex.h>
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.