473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10766
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.c om (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*******@free net.de)
Nov 13 '05 #3
Irrwahn Grausewitz wrote:
ne*****@tokyo.c om (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****@jdyallo p.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*******@free net.de)
Nov 13 '05 #5

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

Similar topics

2
32086
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 complex type: <s:complexContent><s:restriction base="SOAP-ENC:Array"><s:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string"/></s:complexContent> Actual Output :
0
1577
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 a complex structure (complex type). Probably xs:anytype is what I need, but I miss something... In the following schema, I define a "genericchild" node, which I'd want to be able to contain any (complex type) node I want to put in there: :...
9
2514
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> </CONFIG_INFO>
7
2928
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 out to create a "DataSet" as the return type from the webservice?
1
2669
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 response of the web service, what seems so be the type "hashtable" in .NET. Every "Item" consists of two fields "key" and "value". I read a lot about SOAP/Apache/.NET/Complex Types/etc. and somewhere I read that there is no automatic deserialization...
3
3678
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 - "TextField" and "FileChooser". Here is definition of "TextField" <xsd:complexType name="TextField">
0
2611
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 http://emle.sourceforge.net/emle020100/lab/ng20070621_emle_lab_011.xml pass these validation tests with no errors: http://syseng.nist.gov/b2bTestbed/projects/xmlvalidation/schema_validation.html...
0
1363
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 enhance this samples to support complex type or profile property with a group it though an error regarding deserializer. How can we work WCF profile service with complex type?
0
1645
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 using the custom type "requiredstring" defined as: <xs:simpleType name="requiredstring"> <xs:restriction base="xs:string">
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.