473,796 Members | 2,569 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
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.c om wrote:
>
Morris Dovey <mr*****@iedu.c omwrites:
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.c omwrites:
bu************* ****@AM.gmail.c om wrote:
>>
Morris Dovey <mr*****@iedu.c omwrites:
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**********@a ioe.org>, jacob navia <ja***@nospam.o rgwrote:
>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.n rc-cnrc.gc.caschre ef in bericht
news:fq******** **@canopus.cc.u manitoba.ca...
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'.
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<do ublec(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
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...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.