math.h functions for type _Complex

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

    math.h functions for type _Complex

    #include <stdio.h>
    #include <stdlib.h>
    #include <complex.h>
    int main(void)
    {
    _Complex z1, z2, z3;
    z1 = .4 + .7i;
    z2 = pow(z1, 2);
    z3 = z1 * z1;
    printf("%f, %f \n", z1);
    printf("%f, %f \n", z2);
    printf("%f, %f \n", z3);
    system("PAUSE") ;
    return 0;
    }
    In this program, z2 is not equal to z3 . z3 looks correct. On my
    implementation (devcpp on its maiden voyage), z2 becomes the real part
    squared, and the complex part zeros out and is probably undefined.

    Q1)With C99's new types, how does one use the old math functions?

    Q2)Is complex or _Complex a better type defn? LS


  • attn.steven.kuo@gmail.com

    #2
    Re: math.h functions for type _Complex

    On Feb 1, 1:18 pm, "Lane Straatman" <inva...@invali d.netwrote:
    #include <stdio.h>
    #include <stdlib.h>
    #include <complex.h>
    int main(void)
    {
    _Complex z1, z2, z3;
    z1 = .4 + .7i;
    z2 = pow(z1, 2);
    z3 = z1 * z1;
    printf("%f, %f \n", z1);
    printf("%f, %f \n", z2);
    printf("%f, %f \n", z3);
    system("PAUSE") ;
    return 0;}
    >
    In this program, z2 is not equal to z3 . z3 looks correct. On my
    implementation (devcpp on its maiden voyage), z2 becomes the real part
    squared, and the complex part zeros out and is probably undefined.
    >
    Q1)With C99's new types, how does one use the old math functions?

    You should use math functions designed to work with complex operands.
    For example, instead of pow, use cpow.
    Q2)Is complex or _Complex a better type defn? LS
    The former.

    Try something like:

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

    int main (void)
    {
    double complex z1, z2, z3;
    z1 = .4 + .7I;
    z2 = cpow(z1, 2.0);
    z3 = z1 * z1;
    printf("%lf %lf\n", creal(z1), cimag(z1));
    printf("%lf %lf\n", creal(z2), cimag(z2));
    printf("%lf %lf\n", creal(z3), cimag(z3));
    return EXIT_SUCCESS;
    }


    --
    Hope this helps,
    Steven

    Comment

    • Lane Straatman

      #3
      Re: math.h functions for type _Complex


      <attn.steven.ku o@gmail.comwrot e in message
      news:1170366991 .801512.142200@ l53g2000cwa.goo glegroups.com.. .
      On Feb 1, 1:18 pm, "Lane Straatman" <inva...@invali d.netwrote:
      >Q1)With C99's new types, how does one use the old math functions?
      You should use math functions designed to work with complex operands.
      For example, instead of pow, use cpow.
      Thanks for your reply. I was completely ignorant of cpow's existence.
      >Q2)Is complex or _Complex a better type defn? LS
      The former.
      I think so too.
      Try something like:
      [refines source with better type defns and output]
      #include <stdio.h>
      #include <stdlib.h>
      #include <complex.h>
      #include <stdbool.h>
      #define N 41
      #ifdef __bool_true_fal se_are_defined
      #define N 42
      #endif
      int main (void)
      {
      double complex z1, z2, z3;
      bool flag;
      z1 = .4 + .7I;
      z2 = cpow(z1, 2.0);
      z3 = z1 * z1;
      flag = false;
      flag = true;
      if (flag)
      {
      printf("%lf %lf\n", creal(z1), cimag(z1));
      printf("%lf %lf\n", creal(z2), cimag(z2));
      printf("%lf %lf\n", creal(z3), cimag(z3));
      printf("%d\n", N);
      }
      system("PAUSE") ;
      return EXIT_SUCCESS;
      }
      This program is of no great profoundness; I don't know of any way to get
      used to new aspects of syntax without simply plodding ahead beginning with
      elementary-looking stuff. It's a bit of a trick just to get the headers
      included. LS


      Comment

      • Keith Thompson

        #4
        Re: math.h functions for type _Complex

        "Lane Straatman" <invalid@invali d.netwrites:
        #include <stdio.h>
        #include <stdlib.h>
        You don't use anything in <stdlib.h>.
        #include <complex.h>
        int main(void)
        {
        _Complex z1, z2, z3;
        _Complex is not a type; the complex types are "float _Complex",
        "double _Complex", and "long double _Complex". If your compiler
        allows _Complex by itself, it may be due to a misunderstandin g by its
        authors; there's an incorrect example in the standard. C99 6.7.8
        Example 1 is:

        int i = 3.5;
        complex c = 5 + 3 * I;

        which is illegal. N1124 corrects this to:

        int i = 3.5;
        double complex c = 5 + 3 * I;
        z1 = .4 + .7i;
        z2 = pow(z1, 2);
        You want cpow, not pow.
        z3 = z1 * z1;
        printf("%f, %f \n", z1);
        printf("%f, %f \n", z2);
        printf("%f, %f \n", z3);
        A format of "%f, %f \n" requires two arguments of type double. You're
        giving it a single argument of a complex type, which invokes undefined
        behavior. There's are printf formats for complex numbers; you need to
        print the real and complex parts separately, using the creal() and
        cimag() functions.
        system("PAUSE") ;
        I get:

        sh: PAUSE: command not found
        return 0;
        }
        In this program, z2 is not equal to z3 . z3 looks correct. On my
        implementation (devcpp on its maiden voyage), z2 becomes the real part
        squared, and the complex part zeros out and is probably undefined.
        >
        Q1)With C99's new types, how does one use the old math functions?
        <complex.hdecla res math functions for complex types.
        Q2)Is complex or _Complex a better type defn? LS
        The new keyword is spelled "_Complex" only to avoid breaking existing
        code. If you #include <complex.h>", you get "complex" as a macro that
        expands to "_Complex". "_Complex" is almost deliberately ugly;
        there's no need to use it in user code. Just use "complex" -- or
        rather "double complex".

        Here's a corrected version of your program:

        #include <stdio.h>
        #include <complex.h>
        int main(void)
        {
        double complex z1, z2, z3;
        z1 = .4 + .7i;
        z2 = cpow(z1, 2);
        z3 = z1 * z1;
        printf("%f, %f \n", creal(z1), cimag(z1));
        printf("%f, %f \n", creal(z2), cimag(z1));
        printf("%f, %f \n", creal(z3), cimag(z1));
        return 0;
        }

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Ben Pfaff

          #5
          Re: math.h functions for type _Complex

          Keith Thompson <kst-u@mib.orgwrites :
          "Lane Straatman" <invalid@invali d.netwrites:
          > z2 = pow(z1, 2);
          >
          You want cpow, not pow.
          I think that, if he additionally includes <tgmath.h>, he can use
          plain "pow" here.
          --
          "C has its problems, but a language designed from scratch would have some too,
          and we know C's problems."
          --Bjarne Stroustrup

          Comment

          • lane straatman

            #6
            Re: math.h functions for type _Complex

            On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
            "Lane Straatman" <inva...@invali d.netwrites:
            #include <stdio.h>
            #include <stdlib.h>
            >
            You don't use anything in <stdlib.h>.
            There's a system call.
            >
            #include <complex.h>
            int main(void)
            {
            _Complex z1, z2, z3;
            >
            _Complex is not a type; the complex types are "float _Complex",
            "double _Complex", and "long double _Complex".
            The problem wasn't my compiler. It was the manner in which I read
            item 13 off a recent screenshot. The complex type is what is left
            after you subtract '_Complex' and ';' from the line in which the type
            compiles.


            If your compiler
            allows _Complex by itself, it may be due to a misunderstandin g by its
            authors; there's an incorrect example in the standard. C99 6.7.8
            Example 1 is:
            >
            int i = 3.5;
            complex c = 5 + 3 * I;
            >
            which is illegal. N1124 corrects this to:
            >
            int i = 3.5;
            double complex c = 5 + 3 * I;
            I started with 3 and 5 but ended with 4 and 7. I was unaware of
            cpow. Ben is right that tgmath.h would have overloaded operators that
            would still let me use pow(), but the idea is that we're all going to
            use the things that C99 offers, in particular, for complex
            exponentiation, cpow().

            z1 = .4 + .7i;
            z2 = pow(z1, 2);
            >
            You want cpow, not pow.
            >
            z3 = z1 * z1;
            printf("%f, %f \n", z1);
            printf("%f, %f \n", z2);
            printf("%f, %f \n", z3);
            >
            A format of "%f, %f \n" requires two arguments of type double. You're
            giving it a single argument of a complex type, which invokes undefined
            behavior. There's are printf formats for complex numbers; you need to
            print the real and complex parts separately, using the creal() and
            cimag() functions.
            >
            system("PAUSE") ;
            >
            I get:
            >
            sh: PAUSE: command not found
            >
            return 0;
            }
            In this program, z2 is not equal to z3 . z3 looks correct. On my
            implementation (devcpp on its maiden voyage), z2 becomes the real part
            squared, and the complex part zeros out and is probably undefined.
            >
            Q1)With C99's new types, how does one use the old math functions?
            >
            <complex.hdecla res math functions for complex types.
            >
            Q2)Is complex or _Complex a better type defn? LS
            >
            The new keyword is spelled "_Complex" only to avoid breaking existing
            code. If you #include <complex.h>", you get "complex" as a macro that
            expands to "_Complex". "_Complex" is almost deliberately ugly;
            there's no need to use it in user code. Just use "complex" -- or
            rather "double complex".
            >
            Here's a corrected version of your program:
            >
            #include <stdio.h>
            #include <complex.h>
            int main(void)
            {
            double complex z1, z2, z3;
            z1 = .4 + .7i;
            z2 = cpow(z1, 2);
            z3 = z1 * z1;
            printf("%f, %f \n", creal(z1), cimag(z1));
            printf("%f, %f \n", creal(z2), cimag(z1));
            printf("%f, %f \n", creal(z3), cimag(z1));
            return 0;
            >
            }
            >
            The above is basically what steven posted, beating your punch in
            cyberspace, to judge from the time. The number 13 reference was to a
            screenshot I made in the context of 1142.pdf that I think has
            relevance now. I'm posting from google and feel like Mr. Magoo. LS

            Comment

            • Keith Thompson

              #7
              Re: math.h functions for type _Complex

              "lane straatman" <grumpy196884@n etzero.netwrite s:
              On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
              "Lane Straatman" <inva...@invali d.netwrites:
              #include <stdio.h>
              #include <stdlib.h>
              You don't use anything in <stdlib.h>.
              There's a system call.
              You're right, my mistake. I deleted the system("PAUSE") ; call from my
              copy of your program, since it the PAUSE command is non-portable.
              #include <complex.h>
              int main(void)
              {
              _Complex z1, z2, z3;
              _Complex is not a type; the complex types are "float _Complex",
              "double _Complex", and "long double _Complex".
              The problem wasn't my compiler. It was the manner in which I read
              item 13 off a recent screenshot. The complex type is what is left
              after you subtract '_Complex' and ';' from the line in which the type
              compiles.
              Sorry, I can't figure out what you mean here.

              The declaration you posted:

              _Complex z1, z2, z3;

              is illegal. Some compilers (including gcc 4.1.1) accept it. I
              suspect they do so because of the incorrect exapmle in the C99
              standard, corrected in n1124.pdf.

              Was the code you posted not the actual code you compiled?

              [...]
              The above is basically what steven posted, beating your punch in
              cyberspace, to judge from the time. The number 13 reference was to a
              screenshot I made in the context of 1142.pdf that I think has
              relevance now. I'm posting from google and feel like Mr. Magoo. LS
              Not having seen your screenshot, I still have no idea what "13" refers
              to. Would you care to clarify?

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Lane Straatman

                #8
                Re: math.h functions for type _Complex


                "Keith Thompson" <kst-u@mib.orgwrote in message
                news:lnveikt0no .fsf@nuthaus.mi b.org...
                "lane straatman" <grumpy196884@n etzero.netwrite s:
                >On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
                _Complex is not a type; the complex types are "float _Complex",
                "double _Complex", and "long double _Complex".
                >The problem wasn't my compiler. It was the manner in which I read
                >item 13 off a recent screenshot. The complex type is what is left
                >after you subtract '_Complex' and ';' from the line in which the type
                >compiles.
                Sorry, I can't figure out what you mean here.

                I think there's a complex type of long double. If there isn't, it would
                shed some light on the issue at hand. LS

                [cut and added]
                Not having seen your screenshot, I still have no idea what "13" refers
                to. Would you care to clarify?
                I love the quality of the standard that makes it look like print. LS


                Comment

                • Keith Thompson

                  #9
                  Re: math.h functions for type _Complex

                  "Lane Straatman" <invalid@invali d.netwrites:
                  "Keith Thompson" <kst-u@mib.orgwrote in message
                  news:lnveikt0no .fsf@nuthaus.mi b.org...
                  "lane straatman" <grumpy196884@n etzero.netwrite s:
                  On Feb 1, 5:10 pm, Keith Thompson <k...@mib.orgwr ote:
                  _Complex is not a type; the complex types are "float _Complex",
                  "double _Complex", and "long double _Complex".
                  The problem wasn't my compiler. It was the manner in which I read
                  item 13 off a recent screenshot. The complex type is what is left
                  after you subtract '_Complex' and ';' from the line in which the type
                  compiles.
                  Sorry, I can't figure out what you mean here.

                  I think there's a complex type of long double. If there isn't, it would
                  shed some light on the issue at hand. LS
                  n1124 is a PDF document. You should be able to copy-and-paste text
                  from the document and post it here, rather than having to set up a web
                  page with a jpg image of a screenshot.

                  The screenshot was a copy of paragraphs 11 through 13 of section 6.2.5
                  of n1124.pdf:

                  11 There are three _complex types_, designated as float _Complex,
                  double _Complex, and long double _Complex. The real floating and
                  complex types are collectively called the _floating types_.

                  12 For each floating type there is a _corresponding real type_, which
                  is always a real floating type. For real floating types, it is the
                  same type. For complex types, it is the type given by deleting the
                  keyword _Complex from the type name.

                  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.

                  The phrase "a complex type of long double" doesn't make much sense,
                  I'm afraid. There is a type "long double _Complex"; its
                  "correspond ing real type" is long double. (long double itself, of
                  course, is not a complex type.) There is no type "_Complex", though
                  some compilers may allow it.

                  If you have a "#include <complex.h>", the type "long double _Complex"
                  can be referred to as "long double complex" -- and in fact that would
                  be the normal way to refer to it.

                  I fail to see how this sheds any light on the issue at hand -- perhaps
                  because I've completely lost track of what the issue at hand might be.
                  The original program you posted had several errors; I think they've
                  all been pointed out. Did you have any other questions, or is
                  everything sufficiently clear now?

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Lane Straatman

                    #10
                    Re: math.h functions for type _Complex


                    "Keith Thompson" <kst-u@mib.orgwrote in message
                    news:lnlkjfu930 .fsf@nuthaus.mi b.org...
                    "Lane Straatman" <invalid@invali d.netwrites:
                    [...]
                    If you have a "#include <complex.h>", the type "long double _Complex"
                    can be referred to as "long double complex" -- and in fact that would
                    be the normal way to refer to it.
                    >
                    I fail to see how this sheds any light on the issue at hand -- perhaps
                    because I've completely lost track of what the issue at hand might be.
                    The original program you posted had several errors; I think they've
                    all been pointed out. Did you have any other questions, or is
                    everything sufficiently clear now?
                    I have a certain reverence for the integers; I'm not alone in my belief that
                    they came in from on high and then we figured everything else out. C at the
                    onset had an integer type of int. I think there's eleven integer types.
                    There might be a dozen. I think C99 admits of a single boolean type 'bool'.
                    It would be tied with any other type, as far as how new it is to C.

                    Are there complex types in C? If so, how many? LS


                    Comment

                    • CBFalconer

                      #11
                      Re: math.h functions for type _Complex

                      Keith Thompson wrote:
                      >
                      .... snip ...
                      >
                      n1124 is a PDF document. You should be able to copy-and-paste
                      text from the document and post it here, rather than having to
                      set up a web page with a jpg image of a screenshot.
                      You can make it even simpler from a text source. N869_txt.bz2 is
                      such a source, slightly edited for suitability for cut and paste
                      quoting. It's only a 212k download. Available on my page:

                      <http://cbfalconer.home .att.net/download/>

                      --
                      <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>

                      "A man who is right every time is not likely to do very much."
                      -- Francis Crick, co-discover of DNA
                      "There is nothing more amazing than stupidity in action."
                      -- Thomas Matthews

                      Comment

                      • Keith Thompson

                        #12
                        Re: math.h functions for type _Complex

                        "Lane Straatman" <invalid@invali d.netwrites:
                        [...]
                        Are there complex types in C? If so, how many? LS
                        Remember that screenshot you posted upthread,
                        <http://www.billfordx.n et/screendumps/n1142_shot1.htm >?
                        Did you read it? Your question is answered there, quite clearly.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • lane straatman

                          #13
                          Re: math.h functions for type _Complex

                          On Feb 2, 9:50 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                          Keith Thompson wrote:
                          n1124 is a PDF document. You should be able to copy-and-paste
                          text from the document and post it here, rather than having to
                          set up a web page with a jpg image of a screenshot.
                          >
                          You can make it even simpler from a text source. N869_txt.bz2 is
                          such a source, slightly edited for suitability for cut and paste
                          quoting. It's only a 212k download. Available on my page:
                          I don't think filetypes of .bz2 are windows-friendly. I did just
                          install cygwin and would be elated if I could read this in that
                          environment. LS


                          Comment

                          • lane straatman

                            #14
                            Re: math.h functions for type _Complex

                            On Feb 2, 9:02 pm, Keith Thompson <k...@mib.orgwr ote:
                            "Lane Straatman" <inva...@invali d.netwrites:
                            [...]
                            n1124 is a PDF document. You should be able to copy-and-paste text
                            from the document and post it here, rather than having to set up a web
                            page with a jpg image of a screenshot.
                            How?
                            The screenshot was a copy of paragraphs 11 through 13 of section 6.2.5
                            of n1124.pdf:
                            >
                            11 There are three _complex types_, designated as float _Complex,
                            double _Complex, and long double _Complex. The real floating and
                            complex types are collectively called the _floating types_.
                            The screenshot really is better to convey this. "Complex types" is
                            rendered in italics. To answer my question, there do exist complex
                            types. Their cardinality is three.
                            12 For each floating type there is a _corresponding real type_, which
                            is always a real floating type. For real floating types, it is the
                            same type. For complex types, it is the type given by deleting the
                            keyword _Complex from the type name.
                            So we were feeling all good about ourselves after number eleven. Here
                            at number twelve, we're talking about deleting _Complex from a type
                            name, and when we look to the beginning of the sentence we see "for
                            complex types." Can you restate it?
                            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.
                            That's why my output of undefined behavior upthread was correct. It's
                            aligned.

                            Blizzard conditions here in Western Michigan. LS

                            Comment

                            • Keith Thompson

                              #15
                              Re: math.h functions for type _Complex

                              "lane straatman" <grumpy196884@n etzero.netwrite s:
                              On Feb 2, 9:02 pm, Keith Thompson <k...@mib.orgwr ote:
                              "Lane Straatman" <inva...@invali d.netwrites:
                              [...]
                              n1124 is a PDF document. You should be able to copy-and-paste text
                              from the document and post it here, rather than having to set up a web
                              page with a jpg image of a screenshot.
                              How?
                              Probably the same way you'd copy-and-paste anything else from any
                              other application on your OS. The details are OS-specific, and
                              possibly application-specific.
                              The screenshot was a copy of paragraphs 11 through 13 of section 6.2.5
                              of n1124.pdf:

                              11 There are three _complex types_, designated as float _Complex,
                              double _Complex, and long double _Complex. The real floating and
                              complex types are collectively called the _floating types_.
                              The screenshot really is better to convey this. "Complex types" is
                              rendered in italics. To answer my question, there do exist complex
                              types. Their cardinality is three.
                              Which I represented above by adding '_' characters before and after
                              any italic text. It's a little extra work, but since each article is
                              written once and read many times, it's worth it.
                              >
                              12 For each floating type there is a _corresponding real type_, which
                              is always a real floating type. For real floating types, it is the
                              same type. For complex types, it is the type given by deleting the
                              keyword _Complex from the type name.
                              So we were feeling all good about ourselves after number eleven. Here
                              at number twelve, we're talking about deleting _Complex from a type
                              name, and when we look to the beginning of the sentence we see "for
                              complex types." Can you restate it?
                              For example, "double _Complex" is a complex type, and it's
                              "correspond ing real type" is "double" (which, of course, is not a
                              complex type).

                              [snip]

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...