473,387 Members | 1,859 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,387 software developers and data experts.

complex I/O

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.
Nov 13 '05 #1
4 3438
Gerald I. evenden wrote:
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.
Nov 13 '05 #2
Jeremy Yallop wrote:

Gerald I. evenden wrote:
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.


He was asking for _input_ . Do you mean

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

??
--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #3
On Sat, 11 Oct 2003 20:34:52 GMT, "Gerald I. evenden"
<je************@verizon.net> wrote:
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.
{
/* v was successfully assigned -- additional handling may be required
*/
}


<<Remove the del for email>>
Nov 13 '05 #4
"Gerald I. evenden" <je************@verizon.net> writes:
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
Nov 13 '05 #5

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

Similar topics

3
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary...
21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
3
by: Arthur | last post by:
Spending the morning avoiding responsibilities, and seeing what it would take to color some complex numbers. class color_complex(complex): def __init__(self,*args,**kws):...
7
by: Schüle Daniel | last post by:
Hello I am trying to customize the handling of complex numbers what I am missing is a builtin possibility to create complex numbers in polar coordinates so first I wrote a standalone function...
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...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
3
by: Klaas Vantournhout | last post by:
Hi all, I was wondering why there is one extra argument for the return values of complex functions. And why is this not the case with any other data type (except char) example : * In case...
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...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.