Complex numbers and printf

Discussion in 'C Programming' started by jacob navia, Feb 29, 2008.

  1. jacob navia

    jacob navia Guest

    The C99 standard forgot to define the printf equivalent for complex numbers


    Since I am revising the lcc-win implementation of complex numbers
    I decided to fill this hole with
    "Z"

    for instance
    double _Complex m = 2+3*I;
    printf("%Zg\n",m);
    will print
    2+3*I

    The alternative flag makes this look like:

    printf("%#Zg\n",m);
    2.00000+3.00000i

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Feb 29, 2008
    #1
    1. Advertising

  2. jacob navia

    Gerry Ford Guest

    "jacob navia" <> wrote in message
    news:fq8jj0$oll$...

    > The C99 standard forgot to define the printf equivalent for complex
    > numbers
    >
    >
    > Since I am revising the lcc-win implementation of complex numbers
    > I decided to fill this hole with
    > "Z"
    >
    > for instance
    > double _Complex m = 2+3*I;
    > printf("%Zg\n",m);
    > will print
    > 2+3*I
    >
    > The alternative flag makes this look like:
    >
    > printf("%#Zg\n",m);
    > 2.00000+3.00000i

    I don't think it's quite like that, Jacob, much like the dinner I ingested
    on the Champs d'Elyzee. Unlike those 6 chefs 2 dieners, who knew to feed my
    wife excellent cuisine while they fed me a horse's willy that I either gave
    to my shirt pocket or to the sidewalk on our way back to the hotel, I
    actually expected a meal.

    I'd be interested to try a new version of lcc.



    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
     
    Gerry Ford, Feb 29, 2008
    #2
    1. Advertising

  3. jacob navia

    Doug Miller Guest

    In article <>, "Gerry Ford" <> wrote:

    >I don't think it's quite like that, Jacob, much like the dinner I ingested
    >on the Champs d'Elyzee. Unlike those 6 chefs 2 dieners, who knew to feed my
    >wife excellent cuisine while they fed me a horse's willy that I either gave
    >to my shirt pocket or to the sidewalk on our way back to the hotel, I
    >actually expected a meal.


    In future, please sober up before posting. Thank you.
     
    Doug Miller, Feb 29, 2008
    #3
  4. jacob navia

    jacob navia Guest

    Gerry Ford wrote:

    [bad dinner left a lasting impression]

    Coming back to complex numbers... Any comments?



    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Feb 29, 2008
    #4
  5. jacob navia wrote:
    > Gerry Ford wrote:
    >
    > [bad dinner left a lasting impression]
    >
    > Coming back to complex numbers... Any comments?

    Would it give a diagnostic if called in conforming mode?

    Bye, Jojo
     
    Joachim Schmitz, Feb 29, 2008
    #5
  6. jacob navia wrote:
    > Gerry Ford wrote:
    >
    > [bad dinner left a lasting impression]
    >
    > Coming back to complex numbers... Any comments?

    You initial post sounded quite matter-of-factly and not like e request for
    comments...
     
    Joachim Schmitz, Feb 29, 2008
    #6
  7. jacob navia

    jacob navia Guest

    Joachim Schmitz wrote:
    > jacob navia wrote:
    >> Gerry Ford wrote:
    >>
    >> [bad dinner left a lasting impression]
    >>
    >> Coming back to complex numbers... Any comments?

    > Would it give a diagnostic if called in conforming mode?
    >
    > Bye, Jojo
    >
    >


    No.

    I do not think it is necessary since this is an
    illegal combination for printf anyway. It WILL give
    a warning if you do not pass it a complex number
    of course.



    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Feb 29, 2008
    #7
  8. jacob navia

    jacob navia Guest

    Joachim Schmitz wrote:
    > jacob navia wrote:
    >> Gerry Ford wrote:
    >>
    >> [bad dinner left a lasting impression]
    >>
    >> Coming back to complex numbers... Any comments?

    > You initial post sounded quite matter-of-factly and not like e request for
    > comments...
    >
    >


    Excuse me. I should have request comments more explicitely.

    I hope I do not mess something else. I used "Z" since in the lasts
    discussions (about my "b" extension) people complained that it was a
    lower case letter. Then I used now upper case ones.

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Feb 29, 2008
    #8
  9. Joachim Schmitz said:

    > jacob navia wrote:
    >> Gerry Ford wrote:
    >>
    >> [bad dinner left a lasting impression]
    >>
    >> Coming back to complex numbers... Any comments?

    > Would it give a diagnostic if called in conforming mode?


    No diagnostic message is required for invalid format specifiers to printf.
    The behaviour is undefined, and implementations can do anything they like
    as a consequence. In lcc-win32, maybe the implementation will treat the
    argument as a complex number. Perhaps under some other implementation
    it'll format the hard disk. Life gets exciting when you allow your code to
    rely on non-standard extensions and then re-compile the code in a
    different environment.

    I'm not quite sure why this thread is cross-posted to comp.lang.c - it
    seems to be an entirely lcc-related subject.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 29, 2008
    #9
  10. jacob navia

    CBFalconer Guest

    jacob navia wrote:
    >
    > The C99 standard forgot to define the printf equivalent for
    > complex numbers


    I don't believe so. Formatting of such dumps is easily done by
    selecting the real and imaginary portions, and using the existing
    printf operations to format them. Why standardize something that
    will not be used and is unnecessary?

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 29, 2008
    #10
  11. jacob navia

    Nelu Guest

    On Fri, 29 Feb 2008 15:50:17 +0100, jacob navia wrote:

    > Gerry Ford wrote:
    >
    > [bad dinner left a lasting impression]
    >
    > Coming back to complex numbers... Any comments?


    What about complex numbers?


    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Feb 29, 2008
    #11
  12. jacob navia

    Nelu Guest

    On Fri, 29 Feb 2008 10:34:17 +0100, jacob navia wrote:

    > The C99 standard forgot to define the printf equivalent for complex
    > numbers
    >
    >
    > Since I am revising the lcc-win implementation of complex numbers I
    > decided to fill this hole with
    > "Z"
    >
    > for instance
    > double _Complex m = 2+3*I;
    > printf("%Zg\n",m);
    > will print
    > 2+3*I
    >
    > The alternative flag makes this look like:
    >
    > printf("%#Zg\n",m);
    > 2.00000+3.00000i


    Is it worth doing this? I mean is there a good reason to make the
    programmer remember an extra formatting option that's non standard
    instead of allowing him to use what he already knows? I mean doing printf
    ("%f+%f*I\n",real(m),imag(m)) is not that big of an effort, is it?
    However, if you really want to have this formatting option it's probably
    a good idea to talk to the guys in comp.std.c and see if they are
    considering something like that and whether or not your implementation
    may be affected by the standard in the future.




    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Feb 29, 2008
    #12
  13. jacob navia

    Guest

    jacob navia <> wrote:
    >
    > The C99 standard forgot to define the printf equivalent for complex numbers


    No it didn't. The committee made a concious decision *not* to define a
    format for complex numbers since there is no agreed upon format and it's
    easy enough to format the real and imaginary parts yourself however you
    desire.

    -Larry Jones

    I take it there's no qualifying exam to be a Dad. -- Calvin
     
    , Feb 29, 2008
    #13
  14. jacob navia <> writes:
    > The C99 standard forgot to define the printf equivalent for complex numbers


    I doubt that they forgot; they probably just didn't feel it was
    necessary. In my opinion, they were right.

    > Since I am revising the lcc-win implementation of complex numbers
    > I decided to fill this hole with
    > "Z"


    Do you have a similar extension for scanf?

    > for instance
    > double _Complex m = 2+3*I;
    > printf("%Zg\n",m);
    > will print
    > 2+3*I
    >
    > The alternative flag makes this look like:
    >
    > printf("%#Zg\n",m);
    > 2.00000+3.00000i


    I see no problem with this extension in terms of conformance to the
    standard. Though I don't see any particular need for a special format
    for complex numbers, I wouldn't object if such a format were added to
    a future version of the standard.

    However, if I were a user of lcc-win, I probably wouldn't use it in my
    own code, for several reasons.

    First, it is of course an extension. Unless my code already depends
    on other lcc-win extensions, I'd rather not limit its portability for
    the sake of a minor convenience.

    Second, it's inflexible; it imposes one of two output representations
    for complex numbers. If I want to print "2+3*I", I can write:

    printf("%g+%g*I\n", creal(m), cimag(m));

    If I want to print "2 + 3i", of course, I can write:

    printf("%g + %gi\n", creal(m), cimag(m));

    which your extension doesn't support, but the standard already does.

    In effect, I tend to think of a complex number as a number (usually)
    for purposes of arithmetic, but as a composite structure for purposes
    of I/O. Perhaps that's just me.

    Finally Fortran has supported complex numbers, and I/O on them, since
    dinosaurs walked the Earth. The result of printing 2+3*I in Fortran
    would be "(2.,3.)" (at least by default using g77; I don't know
    Fortran well enough to know whether that's standard). That form is
    also valid in Fortran source as a complex literal (insert previous
    disclaimer here).

    Note that a parenthesized format might make things easier for a
    corresponding scanf extension.

    This is not to say that your extension is a bad idea, merely that I
    personally don't find it to be a sufficiently good idea to pique my
    interest. (And yet here I am writing about it at some length.)

    Or, as "Gerry Ford" might put it, "Bertha termite prefecture
    psychopomp inhabit dovetail statuette virus superlative deposition."

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 29, 2008
    #14
  15. In article <fq8jj0$oll$>, jacob navia <> wrote:
    >The C99 standard forgot to define the printf equivalent for complex numbers


    >Since I am revising the lcc-win implementation of complex numbers
    >I decided to fill this hole with
    >"Z"


    Historically, Z was usually associated with hexadecimal
    (but it has been years since I've seen that.)
    --
    "To all, to each! a fair good-night,
    And pleasing dreams, and slumbers light" -- Sir Walter Scott
     
    Walter Roberson, Feb 29, 2008
    #15
  16. In article <>,
    Keith Thompson <> wrote:

    >Second, it's inflexible; it imposes one of two output representations
    >for complex numbers. If I want to print "2+3*I", I can write:


    > printf("%g+%g*I\n", creal(m), cimag(m));


    >If I want to print "2 + 3i", of course, I can write:


    > printf("%g + %gi\n", creal(m), cimag(m));


    >which your extension doesn't support, but the standard already does.


    Then there is engineering, in which the imaginary constant is
    usually represented as 'j' instead of 'i' or 'I'.
    --
    "Style is instinctive and few achieve it in a notable degree. Its
    development is not hastened by instruction. It comes or it doesn't.
    It will take care of itself." -- Walter J. Phillips
     
    Walter Roberson, Feb 29, 2008
    #16
  17. jacob navia

    Nelu Guest

    On Fri, 29 Feb 2008 16:08:59 +0000, Nelu wrote:


    <snip>
    > printf ("%f+%f*I\n",real(m),imag(m)) is not that big of an effort, is


    That's probably creal and cimag. I think the real/imag forms are only
    available in C++.


    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Feb 29, 2008
    #17
  18. jacob navia

    Morris Dovey Guest

    Bartc wrote:

    > But inventing new printf formats is fun; how about the following for
    > printing arrays and blocks of data:
    >
    > int a[] = {10,20,30,40,50,60};
    >
    > printf(" (%A,%d)\n",a,6);
    >
    > will print: (10,20,30,40,50,60)
    >
    > %A means array print, the parameter should point to the data. An extra
    > int/size_t parameter is how many values to print. The format of each element
    > follows (%d). The text between %A and %d is separator chars. Maybe %10A for
    > 10 elems per line.
    >
    > Might get more use than %Z anyway.


    Cool! How about:

    printf("%f",a[i=0; i<6; i++]);

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/Stirling/
     
    Morris Dovey, Feb 29, 2008
    #18
  19. In article <>,
    CBFalconer <> wrote:
    >jacob navia wrote:


    >> The C99 standard forgot to define the printf equivalent for
    >> complex numbers


    >I don't believe so. Formatting of such dumps is easily done by
    >selecting the real and imaginary portions, and using the existing
    >printf operations to format them. Why standardize something that
    >will not be used and is unnecessary?


    Because it gives an opportunity to demonstrate the incomparible
    beauty of operator overloading??
    --
    "To all, to each! a fair good-night,
    And pleasing dreams, and slumbers light" -- Sir Walter Scott
     
    Walter Roberson, Feb 29, 2008
    #19
  20. jacob navia

    jacob navia Guest

    Walter Roberson wrote:
    > In article <>,
    > CBFalconer <> wrote:
    >> jacob navia wrote:

    >
    >>> The C99 standard forgot to define the printf equivalent for
    >>> complex numbers

    >
    >> I don't believe so. Formatting of such dumps is easily done by
    >> selecting the real and imaginary portions, and using the existing
    >> printf operations to format them. Why standardize something that
    >> will not be used and is unnecessary?

    >
    > Because it gives an opportunity to demonstrate the incomparible
    > beauty of operator overloading??


    Operator overloading in printf?

    Funny how much nonsense you can say. Are you paid by the nonsense line?

    Or you do it for free as a hobby?


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Feb 29, 2008
    #20
    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:
    588
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. ben
    Replies:
    4
    Views:
    634
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,278
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    446
  5. guru
    Replies:
    8
    Views:
    290
Loading...

Share This Page