Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > complex type

Reply
Thread Tools

complex type

 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      11-03-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
((E-Mail Removed))
 
Reply With Quote
 
 
 
 
Mantorok Redgormor
Guest
Posts: n/a
 
      11-03-2003
With what specifier do I use to print a variable of a complex type?
The man page for printf doesn't even say.


- nethlek
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-03-2003
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

 
Reply With Quote
 
Jeremy Yallop
Guest
Posts: n/a
 
      11-03-2003
Irrwahn Grausewitz wrote:
> (E-Mail Removed) (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.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      11-03-2003
Jeremy Yallop <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
How do I restrict the type of a text node in a mixed, complex-type element? Chishun Kwong XML 0 03-03-2005 05:09 PM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM
element of complex type without type="" possible? Ulf Heyder XML 1 10-28-2003 04:30 PM



Advertisments