Velocity Reviews > Complex numbers and printf

Complex numbers and printf

Bartc
Guest
Posts: n/a

 02-29-2008

"jacob navia" <(E-Mail Removed)> wrote in message
news:fq8jj0\$oll\$(E-Mail Removed)...
> 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

Martin Ambuhl
Guest
Posts: n/a

 02-29-2008
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

Guest
Posts: n/a

 02-29-2008
Morris Dovey <(E-Mail Removed)> writes:

> Bartc wrote:

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

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

--
burton

Eric Sosman
Guest
Posts: n/a

 02-29-2008
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.

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

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Guest
Posts: n/a

 02-29-2008
Morris Dovey <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote:
>>
>> Morris Dovey <(E-Mail Removed)> writes:
>>
>> > 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

--
burton

Walter Roberson
Guest
Posts: n/a

 03-01-2008
In article <fq9ijn\$7c1\$(E-Mail Removed)>, jacob navia <(E-Mail Removed)> wrote:
>Walter Roberson wrote:

>> Because it gives an opportunity to demonstrate the incomparible

the matter of whether printf() qualifies as an "operator"
or not.

don't you?

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

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

--
"Prevention is the daughter of intelligence."
-- Sir Walter Raleigh

Serve Laurijssen
Guest
Posts: n/a

 03-01-2008

"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> schreef in bericht
news:fq9ftp\$t3f\$(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> Keith Thompson <(E-Mail Removed)> wrote:
>
>>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));

>

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

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

Noob
Guest
Posts: n/a

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

David Thompson
Guest
Posts: n/a

 03-10-2008
On Fri, 29 Feb 2008 08:16:55 -0800, Keith Thompson <(E-Mail Removed)>
wrote:

> jacob navia <(E-Mail Removed)> writes:

<snip: use modifier Z for complex>
> 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));
>
>
> 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

Gerry Ford
Guest
Posts: n/a

 03-10-2008

"David Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Fri, 29 Feb 2008 08:16:55 -0800, Keith Thompson <(E-Mail Removed)>
> wrote:
>
>> jacob navia <(E-Mail Removed)> writes:

> <snip: use modifier Z for complex>
>> 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));
>>
>>
>> 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

Nuts, Sr. Thompson.

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