473,390 Members | 1,464 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,390 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 10743
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.