complex type

Discussion in 'C Programming' started by Irrwahn Grausewitz, Nov 3, 2003.

  1. (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
    ()
     
    Irrwahn Grausewitz, Nov 3, 2003
    #1
    1. Advertising

  2. With what specifier do I use to print a variable of a complex type?
    The man page for printf doesn't even say.


    - nethlek
     
    Mantorok Redgormor, Nov 3, 2003
    #2
    1. Advertising

  3. 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
     
    Martin Ambuhl, Nov 3, 2003
    #3
  4. Irrwahn Grausewitz wrote:
    > (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.
     
    Jeremy Yallop, Nov 3, 2003
    #4
  5. Jeremy Yallop <> 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
    ()
     
    Irrwahn Grausewitz, Nov 3, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    581
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Ulf Heyder
    Replies:
    1
    Views:
    496
    Patrick TJ McPhee
    Oct 28, 2003
  3. Chishun Kwong
    Replies:
    0
    Views:
    418
    Chishun Kwong
    Mar 3, 2005
  4. Stanimir Stamenkov
    Replies:
    2
    Views:
    749
    Stanimir Stamenkov
    Oct 25, 2005
  5. Robert Mark Bram
    Replies:
    0
    Views:
    686
    Robert Mark Bram
    Feb 4, 2007
Loading...

Share This Page