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

complex type

With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.
- nethlek
Nov 13 '05 #1
4 10740
Mantorok Redgormor wrote:
With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.


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

int main(void)
{
complex c = 5 + 3*I;
printf("%g + %gi\n", creal(c), cimag(c));
return 0;
}

--
Martin Ambuhl

Nov 13 '05 #2
ne*****@tokyo.com (Mantorok Redgormor) wrote:
With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.


Unfortunately there is no printf format conversion specifier for
printing complex numbers directely, but ISO/IEC 9899:1999 says:

6.2.5 Types
[...]
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.
[...]

Thus you can printf the value of an object of complex type like an array
of 2 of the underlying real type, for example:

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

int main( void )
{
double complex cmplvar = 5.0 + 3.0 * I;
printf ("cmplvar = %f+%fi\n", cmplvar[0], cmplvar[1] );
return 0;
}

Untested code; the implementation at hand doesn't support complex types.

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3
Irrwahn Grausewitz wrote:
ne*****@tokyo.com (Mantorok Redgormor) wrote:
With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.


Unfortunately there is no printf format conversion specifier for
printing complex numbers directely, but ISO/IEC 9899:1999 says:

6.2.5 Types
[...]
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.
[...]

Thus you can printf the value of an object of complex type like an array
of 2 of the underlying real type, for example:

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

int main( void )
{
double complex cmplvar = 5.0 + 3.0 * I;
printf ("cmplvar = %f+%fi\n", cmplvar[0], cmplvar[1] );


"cmplvar" is not an array (or pointer), so you can't use the subscript
operator on it, despite the guarantees about representation and
alignment quoted above. You can do nasty things with casts to make
your example work, but creal() and cimag() are the right solution in
this case.

Jeremy.
Nov 13 '05 #4
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote:
Irrwahn Grausewitz wrote:
printf ("cmplvar = %f+%fi\n", cmplvar[0], cmplvar[1] );
"cmplvar" is not an array (or pointer), so you can't use the subscript
operator on it, despite the guarantees about representation and
alignment quoted above. You can do nasty things with casts to make
your example work,

Indeed.
but creal() and cimag() are the right solution in
this case.


Definitely! What was I thinking? Fortunately Martin Ambuhl posted a
correct example. My apologies for posting nonsense.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #5

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

Similar topics

2
by: burdeen | last post by:
I've been trying to return an array with PHP5 soap. Is this not supported? or am i doing it wrong? Seems to return on the last element in the array. In my WSDL i've defined my return type as a...
0
by: AL | last post by:
Hello, working on an xsd schema, I'd like it to have a node of which I don't know the content at design time: I want the document instance to identify such node contents, where content should have...
9
by: Kanchana D S | last post by:
Hi, I have a complex type which has to have its content in the xml instance document as shown below : <CONFIG_INFO> <country resourceNumber="20004" fieldSecurity="5, 5">Great Britain</label>...
7
by: Nalaka | last post by:
Hi, I created a sinple web service that returns a dataSet. Then I created a client program that uses this web service (that returns the Dataset). My question is, how did the client figure...
1
by: Gregor D. | last post by:
Hi, I want to consume an Apache Webservice with a C# .NET Client. Everything works fine, but I'm having problem with the deserialization of the complex type "Item" that is included in the...
3
by: katis | last post by:
Hi all :) Is it posible in xsd to change only minOccurs value of element in complex type by extending this type? The problem I'm trying to solve is this: I have two complex types -...
0
by: C.W.Holeman II | last post by:
I am using Firefox for details see: http://emle.sourceforge.net/emle020100/ng20070620_context.html http://emle.sourceforge.net/emle020100/ng20070621_emle_lab_011.xsd and...
0
by: =?Utf-8?B?R29o?= | last post by:
Any one have an idea Profile Service in WCF with complex type. example from the msdn simple version data type profile work fine. http://msdn.microsoft.com/en-us/library/bb514968.aspx when I...
0
by: sayor | last post by:
Hi, I am writing a schema to check for empty elements in an XML file. I am able to do so for simple elements such as: <xs:element name="authorname" type ="requiredstring"></xs:element> by...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.