complex I/O

G

Gerald I. evenden

I have found that with the Gnu compiler that undocumented output
of a "double comple" value can be successfully performed by:

double complex v;

printf("%g %gi\n",v);

which will print the real and imaginary parts of "v."

However, on input I find no equivalent operation that will
work.

I on forced into reading into two temporary type doubles and
then assigning them to the real and imaginary parts of v??

Harbison & Steele (fifth) are limited on complex documentation.

Am I missing something?

Thanks.
 
J

Jeremy Yallop

Gerald said:
I have found that with the Gnu compiler that undocumented output
of a "double comple" value can be successfully performed by:

double complex v;

printf("%g %gi\n",v);

which will print the real and imaginary parts of "v."

However, on input I find no equivalent operation that will
work.

#include <complex.h>
#include <stdio.h>

printf("%g + %gi\n", creal(v), cimag(v));

Jeremy.
 
J

Julian V. Noble

Jeremy said:
#include <complex.h>
#include <stdio.h>

printf("%g + %gi\n", creal(v), cimag(v));

Jeremy.

He was asking for _input_ . Do you mean

scanf("%g + %gi\n", creal(v), cimag(v));

??
--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
I

Irrwahn Grausewitz

Julian V. Noble said:
He was asking for _input_ . Do you mean

scanf("%g + %gi\n", creal(v), cimag(v));

??

Ouch! The scanf "%g" conversion specifier expects a pointer to a
variable of floating point type. creal and cimag are functions
returning double. How would that even compile?

To OP: AFAICT there's no way (especially no scanf modifier) to read
and convert complex values "on the fly", so most certainly you
have to stick to:
[...] reading into two temporary type doubles and
then assigning them to the real and imaginary parts of v [...]


Irrwahn,
and now back to my cereals...
 
T

those who know me have no need of my name

in comp.lang.c i read:
scanf("%g + %gi\n", creal(v), cimag(v));

this is not possible -- neither of those functions yield lvalues. it is as
gerald fears, there is no pre-defined functionality which converts text to
a complex type. fortunately:

[#13] Each complex type has the same representation and
alignment requirements as an array type containing exactly
two elements of the corresponding real type; the first
element is equal to the real part, and the second element to
the imaginary part, of the complex number.

[my copy of c99 isn't handy, but as i remember it this quote from n869
remained unchanged]

so:

double complex v;
if (2 == scanf("%lg + %lgi", (double*)&v, (double*)&v+1))
{
/* v was successfully assigned -- additional handling may be required */
}
 
J

Jeremy Yallop

Julian said:
He was asking for _input_ . Do you mean

scanf("%g + %gi\n", creal(v), cimag(v));

No, I was just pointing out that there is a standard way of producing
the "undocumented output" that the OP mentioned. I missed the request
for an input equivalent (which can be done by treating the complex
object as an array of two doubles, FWIW).

Jeremy.
 
G

Gerald I. evenden

in comp.lang.c i read:
scanf("%g + %gi\n", creal(v), cimag(v));

this is not possible -- neither of those functions yield lvalues. it is
as gerald fears, there is no pre-defined functionality which converts text
to
a complex type. fortunately:

[#13] Each complex type has the same representation and
alignment requirements as an array type containing exactly
two elements of the corresponding real type; the first
element is equal to the real part, and the second element to
the imaginary part, of the complex number.

Yes, yes. I do remember reading that but it didn't sink in.
[my copy of c99 isn't handy, but as i remember it this quote from n869
remained unchanged]

so:

double complex v;
if (2 == scanf("%lg + %lgi", (double*)&v, (double*)&v+1))

Alternatively, it may be written as:

scanf("%lg %lg",&(double)value,&(double)value+1)

or

scanf("%lg %lg",&value,&(double)value+1)
 
K

Keith Thompson

Gerald I. evenden said:
I have found that with the Gnu compiler that undocumented output
of a "double comple" value can be successfully performed by:

double complex v;

printf("%g %gi\n",v);

which will print the real and imaginary parts of "v."

I suspect that's just an accident of the calling convention. It also
depends on the interaction between the compiler and the C runtime
library. It could just as easily print the imaginary part first, or
make demons fly out your nose.

If you turn on warnings, gcc will complain about it.
 
B

Barry Schwarz

in comp.lang.c i read:
scanf("%g + %gi\n", creal(v), cimag(v));

this is not possible -- neither of those functions yield lvalues. it is
as gerald fears, there is no pre-defined functionality which converts text
to
a complex type. fortunately:

[#13] Each complex type has the same representation and
alignment requirements as an array type containing exactly
two elements of the corresponding real type; the first
element is equal to the real part, and the second element to
the imaginary part, of the complex number.

Yes, yes. I do remember reading that but it didn't sink in.
[my copy of c99 isn't handy, but as i remember it this quote from n869
remained unchanged]

so:

double complex v;
if (2 == scanf("%lg + %lgi", (double*)&v, (double*)&v+1))

Alternatively, it may be written as:

scanf("%lg %lg",&(double)value,&(double)value+1)

I don't think so but I don't know the terminology to explain it
properly. On an intuitive level, casting a variable may cause the
"result" to be stored in a temporary compiler generated unnamed
variable of the desired type. Taking the address of this temporary
will not produce the address of the original variable (it might not
even be legal syntax) so scanf will store the result in the wrong
place. Both arguments suffer from this.
or

scanf("%lg %lg",&value,&(double)value+1)

Here only the last argument has the problem described above.

The first argument has a type problem. %lg requires a pointer to
double. value is not a double. &value has type pointer to double.
But a pointer to value my have a different representation than a
pointer to double. If it does, this invokes undefined behavior.



<<Remove the del for email>>
 
M

Micah Cowan

Gerald I. evenden said:
I have found that with the Gnu compiler that undocumented output
of a "double comple" value can be successfully performed by:

double complex v;

printf("%g %gi\n",v);

which will print the real and imaginary parts of "v."

Don't do this. Jeremy has pointed out a better way. The above is
absolutely unportable, and dangerous.

-Micah
 

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

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top