Complex number calculation using library

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

    Complex number calculation using library

    Hi,

    I have this C code which does complex number arithmetic. When I try to
    write a similar file for C++, it says that "creal’ was not declared in
    this scope". Can anyone give a better idea to write it using
    "complex.h" , other than using own data structures?

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

    int main(){

    // double pi = 4*atan2(1);

    double complex A = 32 + 24*I;
    double complex B = 64 + 48*I;
    double complex sum = A + B;

    printf("\na is %f + %fi\n",creal(A) , cimag(A));
    printf("b is %f + %fi\n",creal(B) , cimag(B));
    printf("sum is %f + %fi\n",creal(su m), cimag(sum));
    printf("sum: abs = %f , angle = %f\n", cabs(sum), carg(sum));

    return(0);

    }


    Thanks,
    Reshmi
  • Ian Collins

    #2
    Re: Complex number calculation using library

    Reshmi wrote:
    Hi,
    >
    I have this C code which does complex number arithmetic. When I try to
    write a similar file for C++, it says that "creal’ was not declared in
    this scope". Can anyone give a better idea to write it using
    "complex.h" , other than using own data structures?
    >
    #include <stdio.h>
    #include <complex.h>
    >
    C++ does not include C99's _Complex type.

    Use std::complex from <complex>

    --
    Ian Collins.

    Comment

    • Kai-Uwe Bux

      #3
      Re: Complex number calculation using library

      Reshmi wrote:
      Hi,
      >
      I have this C code which does complex number arithmetic. When I try to
      write a similar file for C++, it says that "creal? was not declared in
      this scope". Can anyone give a better idea to write it using
      "complex.h" , other than using own data structures?
      >
      #include <stdio.h>
      #include <complex.h>
      >
      int main(){
      >
      // double pi = 4*atan2(1);
      >
      double complex A = 32 + 24*I;
      double complex B = 64 + 48*I;
      double complex sum = A + B;
      >
      printf("\na is %f + %fi\n",creal(A) , cimag(A));
      printf("b is %f + %fi\n",creal(B) , cimag(B));
      printf("sum is %f + %fi\n",creal(su m), cimag(sum));
      printf("sum: abs = %f , angle = %f\n", cabs(sum), carg(sum));
      >
      return(0);
      >
      }
      #include <complex>
      #include <iostream>

      typedef std::complex< double double_complex;

      int main ( void ) {
      double_complex A ( 32, 24 );
      double_complex B ( 64, 48 );
      double_complex sum = A + B;
      std::cout << "A is " << A << '\n';
      std::cout << "B is " << B << '\n';
      std::cout << "sum is " << sum << '\n';
      std::cout << "sum: abs = " << std::abs( sum )
      << ", angle = " << std::arg( sum ) << '\n';
      }


      Best

      Kai-Uwe Bux

      Comment

      • James Kanze

        #4
        Re: Complex number calculation using library

        On Sep 20, 4:57 am, Ian Collins <ian-n...@hotmail.co mwrote:
        Reshmi wrote:
        I have this C code which does complex number arithmetic.
        When I try to write a similar file for C++, it says that
        "creal’ was not declared in this scope". Can anyone give a
        better idea to write it using "complex.h" , other than using
        own data structures?
        #include <stdio.h>
        #include <complex.h>
        C++ does not include C99's _Complex type.
        Use std::complex from <complex>
        Or, given his code, compile the program as C (and ask any
        further questions about it in comp.lang.c, of course).

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Reshmi

          #5
          Re: Complex number calculation using library

          Hi James,

          This is part of a bigger function. All other files are in C++. So, I
          wanted to write this function in C++ as well.

          Reshmi


          On Sep 20, 4:25 am, James Kanze <james.ka...@gm ail.comwrote:
          On Sep 20, 4:57 am, Ian Collins <ian-n...@hotmail.co mwrote:
          >
          Reshmi wrote:
          I have this C code which does complex number arithmetic.
          When I try to write a similar file for C++, it says that
          "creal’ was not declared in this scope". Can anyone give a
          better idea to write it using "complex.h" , other than using
          own data structures?
          #include <stdio.h>
          #include <complex.h>
          C++ does not include C99's _Complex type.
          Use std::complex from <complex>
          >
          Or, given his code, compile the program as C (and ask any
          further questions about it in comp.lang.c, of course).
          >
          --
          James Kanze (GABI Software) email:james.ka. ..@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...