Complex Style

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fumeur

    Complex Style


    According to my understanding of the standard, both of

    _Complex double c1; (1)
    double _Complex c2; (2)

    should be syntactically correct and equivalent. Now, there's
    been a recent thread here according to which (1) is not
    accepted by at least one major compiler. On the other hand,
    in existing code I'm working on I have almost exclusively
    seen (1) and very rarely (2); gcc's documentation also uses
    (1) and never (2).

    Is my assumption above correct that both styles are legal
    in strictly conforming code?

    If yes, which style is preferable, seeing that not all
    compilers implement both styles?

  • Flash Gordon

    #2
    Re: Complex Style

    Fumeur wrote, On 15/06/08 23:46:
    According to my understanding of the standard, both of
    >
    _Complex double c1; (1)
    double _Complex c2; (2)
    >
    should be syntactically correct and equivalent.
    They are.
    Now, there's
    been a recent thread here according to which (1) is not
    accepted by at least one major compiler.
    Define major. I had never heard of the compiler in question (if it is
    the one I suspect) until I joined this group. MSVC and gcc, on the other
    hand, I was aware of.
    On the other hand,
    in existing code I'm working on I have almost exclusively
    seen (1) and very rarely (2); gcc's documentation also uses
    (1) and never (2).
    So? Neither gcc documentation (specifically its example) nor examples in
    the standard are proof of what is legal.
    Is my assumption above correct that both styles are legal
    in strictly conforming code?
    They are both acceptable in a strictly conforming C99 program.
    If yes, which style is preferable, seeing that not all
    compilers implement both styles?
    Personally I would use "_Complex double" (or maybe just "_Complex" if
    that is legal) because to me it "sounds" better. If, however, programs
    using _Complex tend to do it differently then I would switch to however
    the majority did it for consistency.
    --
    Flash Gordon

    Comment

    • Keith Thompson

      #3
      Re: Complex Style

      Flash Gordon <spam@flash-gordon.me.ukwri tes:
      Fumeur wrote, On 15/06/08 23:46:
      >According to my understanding of the standard, both of
      > _Complex double c1; (1)
      > double _Complex c2; (2)
      >should be syntactically correct and equivalent.
      >
      They are.
      >
      > Now, there's been a recent thread here according to which (1) is
      >not accepted by at least one major compiler.
      >
      Define major. I had never heard of the compiler in question (if it is
      the one I suspect) until I joined this group. MSVC and gcc, on the
      other hand, I was aware of.
      >
      > On the other hand, in existing code I'm working on I have almost
      >exclusively seen (1) and very rarely (2); gcc's documentation also
      >uses (1) and never (2).
      >
      So? Neither gcc documentation (specifically its example) nor examples
      in the standard are proof of what is legal.
      The standard refers to "float _Complex", "double _Complex", and "long
      double _Complex" in both 6.2.5 (types) and 6.7.2 (type specifiers).
      Of course other orders are legal (including "double _Complex long if
      you want to be especially perverse, but please don't).
      >Is my assumption above correct that both styles are legal
      >in strictly conforming code?
      >
      They are both acceptable in a strictly conforming C99 program.
      >
      >If yes, which style is preferable, seeing that not all compilers
      >implement both styles?
      >
      Personally I would use "_Complex double" (or maybe just "_Complex" if
      that is legal) because to me it "sounds" better. If, however, programs
      using _Complex tend to do it differently then I would switch to
      however the majority did it for consistency.
      No, just "_Complex" is not legal. (An example in C99 6.7.8p24 has
      complex c = 5 + 3 * I;
      This was corrected in one of the Technical Corrigenda.)

      I'd follow the standard's usage (and I don't know why gcc's
      documentation doesn't) unless there was some particular reason not to,
      such as consistency with existing code.

      I'd also add "#include <complex.h>" and use "complex" rather than
      "_Complex".

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Gestorm

        #4
        Re: Complex Style

        Fumeur wrote:
        According to my understanding of the standard, both of
        >
        _Complex double c1; (1)
        double _Complex c2; (2)
        >
        should be syntactically correct and equivalent. Now, there's
        been a recent thread here according to which (1) is not
        accepted by at least one major compiler.
        Hey, I test the following code:
        /*file name is complex.c*/
        #include<stdio. h>
        int main()
        {
        double _Complex a = 1.5;
        _Complex double b = 1.6;
        printf("%f\n", a);
        printf("%f\n", b);
        }
        and compile it buy command
        $gcc complex.c
        Then execute ./a.out
        the result is:
        1.500000
        1.600000
        No error occurs.
        ^_^

        Comment

        • Keith Thompson

          #5
          Re: Complex Style

          Gestorm <zhcfreesea@126 .comwrites:
          Fumeur wrote:
          >According to my understanding of the standard, both of
          >>
          > _Complex double c1; (1)
          > double _Complex c2; (2)
          >>
          >should be syntactically correct and equivalent. Now, there's
          >been a recent thread here according to which (1) is not
          >accepted by at least one major compiler.
          >
          Hey, I test the following code:
          /*file name is complex.c*/
          #include<stdio. h>
          int main()
          {
          double _Complex a = 1.5;
          _Complex double b = 1.6;
          printf("%f\n", a);
          printf("%f\n", b);
          }
          and compile it buy command
          $gcc complex.c
          Then execute ./a.out
          the result is:
          1.500000
          1.600000
          No error occurs.
          gcc was not the "major compiler" in question, so this isn't
          surprising.

          But you're printing values of type double _Complex using a "%f"
          format, which requires an argument of type double.

          Here's a corrected version:

          #include <stdio.h>
          #include <complex.h>

          int main(void)
          {
          double _Complex a = 1.5;
          _Complex double b = 1.6;
          printf("a = %f + %f*I\n", creal(a), cimag(a));
          printf("b = %f + %f*I\n", creal(b), cimag(b));
          return 0;
          }

          The output is:

          a = 1.500000 + 0.000000*I
          b = 1.600000 + 0.000000*I

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Flash Gordon

            #6
            Re: Complex Style

            Keith Thompson wrote, On 16/06/08 01:47:
            Flash Gordon <spam@flash-gordon.me.ukwri tes:
            >Fumeur wrote, On 15/06/08 23:46:
            <snip>
            >Personally I would use "_Complex double" (or maybe just "_Complex" if
            >that is legal) because to me it "sounds" better. If, however, programs
            >using _Complex tend to do it differently then I would switch to
            >however the majority did it for consistency.
            >
            No, just "_Complex" is not legal. (An example in C99 6.7.8p24 has
            complex c = 5 + 3 * I;
            This was corrected in one of the Technical Corrigenda.)
            >
            I'd follow the standard's usage (and I don't know why gcc's
            documentation doesn't) unless there was some particular reason not to,
            such as consistency with existing code.
            Ah well, my preference is not strong, so I may well do the same if I
            ever end up using the complex types. I can certainly see the advantage
            of following the way the standard expresses it.
            I'd also add "#include <complex.h>" and use "complex" rather than
            "_Complex".
            I may well also do that if I ever use the type.
            --
            Flash Gordon

            Comment

            • Serve Lau

              #7
              Re: Complex Style


              "Fumeur" <f6@invalid.inv alidschreef in bericht
              news:5489438.cC 6GMIcojq.f6@inv alid.invalid...
              >
              According to my understanding of the standard, both of
              >
              _Complex double c1; (1)
              double _Complex c2; (2)
              >
              should be syntactically correct and equivalent. Now, there's
              been a recent thread here according to which (1) is not
              accepted by at least one major compiler.
              The world wants to know which compiler

              Comment

              • Fumeur

                #8
                Re: Complex Style


                On Mon, 16 Jun 2008 18:41:16 +0200, "Serve Lau" <nihao@qinqin.c omwrote:
                >
                >"Fumeur" <f6@invalid.inv alidschreef in bericht
                >news:5489438.c C6GMIcojq.f6@in valid.invalid.. .
                >>
                >According to my understanding of the standard, both of
                >>
                > _Complex double c1; (1)
                > double _Complex c2; (2)
                >>
                >should be syntactically correct and equivalent. Now, there's
                >been a recent thread here according to which (1) is not
                >accepted by at least one major compiler.
                >
                >The world wants to know which compiler
                Do your own research!

                Comment

                • Serve Lau

                  #9
                  Re: Complex Style


                  "Fumeur" <f6@invalid.inv alidschreef in bericht
                  news:2645265.Sg AguqdZjN.f6@inv alid.invalid...
                  >
                  On Mon, 16 Jun 2008 18:41:16 +0200, "Serve Lau" <nihao@qinqin.c omwrote:
                  >>The world wants to know which compiler
                  >
                  Do your own research!
                  *cry* never mind then, the world doesnt want to know that bad :P

                  Comment

                  • Fumeur

                    #10
                    Re: Complex Style


                    On Mon, 16 Jun 2008 23:48:17 +0200, "Serve Lau" <nihao@qinqin.c omwrote:
                    >
                    >"Fumeur" <f6@invalid.inv alidschreef in bericht
                    >news:2645265.S gAguqdZjN.f6@in valid.invalid.. .
                    >>
                    >On Mon, 16 Jun 2008 18:41:16 +0200, "Serve Lau" <nihao@qinqin.c omwrote:
                    >
                    >>>The world wants to know which compiler
                    >>
                    >Do your own research!
                    >
                    >*cry* never mind then, the world doesnt want to know that bad :P
                    After reading back through the archives, I think "the world" really
                    may not know because the fact first popped up buried deeply inside
                    one of those homework threads.

                    The relevant article is "<udkmh5-g0r.ln4@localho st.invalid>" wherein
                    a C.L.C reader posted a piece of code using (among others) these two
                    type specifications:

                    _Complex double (1)

                    and

                    double _Complex (2)

                    In a followup article, "<3130137.HWKQ2 RFaLb@aioe.org> ", another
                    reader presented the compiler diagnostics he got on the line(s)
                    containing (1).

                    (being mainly a gcc user, I was a bit surprised by that...)

                    Comment

                    Working...