Thread: Real and Imaginary numbers

  1. #1
    tetra
    Guest

    Real and Imaginary numbers

    ok, i made a program that adds 2 sets of real and imaginary numbers


    like so


    (4, 3.5i) + (5, 2.6i) = (9 , 6.1i)

    ok
    i got all the functions working and everything. the output is the problem, i have all the numbers set to doubles, and it prints out

    -1.07374e+008

    obviously this is wrong once again i set ALL the variables to double

    any help is greatly apreciated.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    are you adding the i's together. that is probably your problem. enter it as string, and parse the string...

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    That's because the quadrahedron of the double duplex modifier is conflicting with the simple semigon convertor. Fix that and everything will be sweet.

    or post some code.
    Joe

  4. #4
    tetra
    Guest

    holy ####...

    come on joe try to keep this english


    ok

    ill post some code


    here is the function page ( i split this up into 3 differnt sorces)
    [CODE]

    #include <iostream>
    using namespace std;

    #include "COMPLEX.H"

    Complex::Complex(int r, double i, int re, double im)
    {
    r = areal;
    i = aimaginary;
    re = breal;
    im = bimaginary;
    }

    Complex::~Complex()
    {

    }

    void Complex::AddR(int areal, int breal)
    {
    int b;
    b = areal + breal;
    }


    void Complex::SubR(int areal,int breal)
    {
    int s;

    s = areal - breal;
    }

    void Complex::AddI( double aimaginary, double bimaginary)
    {
    float l;

    l = aimaginary + bimaginary;

    }
    void Complex::SubI( double aimaginary, double bimaginary)
    {
    float n;
    n = aimaginary - bimaginary;

    }

    void Complex ::displayA()
    {
    cout<<"("<<b<<","<<l<<"i )"<<endl;
    }

    void Complex ::disS()
    {
    cout<<"("<<s<<","<<n<<"i )"<<endl;
    }

    [\CODE]

    And the header file
    [CODE]

    #ifndef COMPLEX_H
    #define COMPLEX_H


    class Complex
    {
    int real;
    double imaginary;
    int sumreal;
    double sumimaginary;
    int areal;
    int breal;
    double aimaginary;
    double bimaginary;
    int b;
    float l;
    int s;
    float n;

    public:
    Complex(int , double ,int ,double);

    ~Complex();
    void AddR(int,int);
    void SubR(int,int);
    void AddI(double,double);
    void SubI(double,double);

    void displayA();
    void disS();
    };



    #endif

    [\CODE]

    and finally the actually running prgram.

    Code:
    #include <iostream>
    using namespace std;
    
    #include "COMPLEX.H"
    
    
    int main()
    {
    
    cout<<"This program will add and subtract\n two predefined sets of real and imaginary numbers\n";
    
    cout<<"(12 , 13.4i) + ( 13, 32.4i)\n\n\n";
    
    cout<<"\n";
    class Complex b(12.3, 13.4, 12.4 ,32.4);
    b.AddR(12, 12.4);
    b.AddI(13, 32.4);
    b.displayA();
    cout<<"\n\n\n";
    cout<<"(12 , 13.4i) - ( 12 , 32.4i)\n\n\n";
    
    b.AddR(12, 12.4);
    b.AddI(13, 32.4);
    b.disS();
    
    
    return 0;
    
    }

    i forgot to mention, this is off windows home xp and msvc++6.0

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>That's because the quadrahedron of the double duplex modifier is conflicting with the simple semigon convertor.
    No no no, the overloaded discombobulator is toggling the enlistment flag too early causing the exponents to miss the bus pass for late binding conciliation. How could you possibly think that a conflict of the double duplex modifier and simple semigon convertor could happen? It's protected by the duplex convertor buffer flag, if the flag is set to NOTLD then the convertor zombies are rancid and have to be reaped before they eat the convertor's brains. So conversions don't even take place :-)
    *Cela*

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    I started going through your code but complex numbers seem to be a bit more complex than I remember them. Why not just have one variable for the real part, and one for the imaginary part? Accept one real and one imaginary parameter in your add/subtract functions, and add and subtract these from your member variables. Unless I've completely missed the point; in which case ignore me and wait until someone more helpful comes along.

    Oh, and ignore Cela because he/she/it is just making stuff up to try and impress the newbies. I mean, who ever heard of a buss pass? WTF is that all about?
    Joe

  7. #7
    tetra
    Guest
    the point is , to add them like this

    (12 , 34.5i) + (2, 32.7i) = (14, 67.2i)


    and why on earth would i get such a crazy output?

    shhesh c++ is SO unpredctable

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I mean, who ever heard of a buss pass? WTF is that all about?
    Don't come crying to me when unreaped zombies start eating your brains :-)

    >>and why on earth would i get such a crazy output?
    Because you're not initializing your variables, they have nothing but garbage in them. The following code fixes all but the final call to disS(), the problem with that one is that you never call a function which sets s or n :-)
    Code:
    #include <iostream>
    using namespace std;
    
    class Complex
    {
      int real;
      double imaginary;
      int sumreal;
      double sumimaginary;
      int areal;
      int breal;
      double aimaginary;
      double bimaginary;
      int b;
      float l;
      int  s;
      float n;
    public:
      Complex(int , double ,int  ,double);
    
      void AddR(int,int);
      void SubR(int,int);
      void AddI(double,double);
      void SubI(double,double);
      
      void displayA();
      void disS();
    };
    
    Complex::Complex(int r, double i, int re, double im)
    {
      areal = r;
      aimaginary = i;
      breal = re;
      bimaginary = im;
    }
    
    void Complex::AddR(int areal, int breal)
    {
      b =  areal + breal;
    }
    
    void Complex::SubR(int areal,int breal)
    {
      s = areal - breal;
    }
    
    void Complex::AddI( double aimaginary, double bimaginary)
    {
      l = aimaginary + bimaginary;
    }
    void Complex::SubI( double aimaginary, double bimaginary)
    {
      n = aimaginary - bimaginary;
    }
    
    void Complex ::displayA()
    {
      cout<<"("<<b<<","<<l<<"i  )"<<endl;
    }
    
    void Complex ::disS()
    {
      cout<<"("<<s<<","<<n<<"i  )"<<endl;
    }
    
    int main()
    {
      cout<<"This program will add and subtract\n"
        "two predefined sets of real and imaginary numbers\n";
      
      cout<<"(12 , 13.4i) + ( 13, 32.4i)\n\n\n";
      
      cout<<"\n";
      Complex b(12.3, 13.4, 12.4 ,32.4);
      b.AddR(12, 12.4);
      b.AddI(13, 32.4);
      b.displayA();
      cout<<"\n\n\n";
      cout<<"(12 , 13.4i) - ( 12 , 32.4i)\n\n\n";
      
      b.AddR(12, 12.4);
      b.AddI(13, 32.4);
      b.disS();
      
      return 0;
    }
    *Cela*

  9. #9
    tetra
    Guest
    hmmm doesnt this declare s and n?

    Code:
    void Complex::SubI( double aimaginary, double bimaginary)
    {
    	n = aimaginary - bimaginary;
    }
    
    // and the s
    
    
    
    void Complex::SubR(int areal,int breal)
    {
      s = areal - breal;
    }


    i appreciate your help.

  10. #10
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>hmmm doesnt this declare s and n?
    Yes it does, but you never call those methods anywhere, so the effect is the same as not even having them :-)
    *Cela*

  11. #11
    tetra
    Guest
    im calling those methods in disS and in the imaginary subtraction and addition numbers see

    Code:
    void Complex ::disS()
    {
      cout<<"("<<s<<","<<n<<"i  )"<<endl;
    }
    
    // doesnt that call them?
    
    void Complex::SubI( double aimaginary, double bimaginary)
    {
    	n = aimaginary - bimaginary;
    }
    
    void Complex::SubR(int areal,int breal)
    {
      s = areal - breal;
    }
    
    // doesnt that use them?

  12. #12
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>im calling those methods in disS and in the imaginary subtraction and addition numbers see
    Nope, you're using s and n in disS(), but since you don't actually call the methods SubR and SubI to give values to s and n, disS() prints garbage. Simply defining a class method doesn't call it. I think you're mistaking your use of the variables with the method call that initializes those variables :-)
    *Cela*

  13. #13
    tetra
    Guest

    omg,....

    ROFL!!!! I MADE A NOOB COPY AND PASTE MISTAKE OMG i spen t almost all night trying to figure out new code to fix this, and alas, it was all a noobie copy and paste error..... this is why i love c++

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Do you know about the built in STL complex template objects? Why build your own?
    Code:
    #include <iostream>
    #include <complex>
    using namespace std;
    
    int main()
    {
        complex<float> c1(4.5,3.1);                    // Real part = 4.5, imaginary = 3.1
        complex<float> c2(2.0,1.0);                    // Real part = 2.0, imaginary = 1.0
    
        cout << "c1: " << c1 << endl;                  // Outputs "c1: (4.5,3.1)"
    
        cout << "c1 + c2: " << c1+c2 << endl;          // Outputs "c1 + c2: (6.5,4.1)"
    
        cout << "c1 real part: " << c1.real() << endl; // Outputs "c1 real part: 4.5"
        cout << "c1 imag part: " << c1.imag() << endl; // Outputs "c1 imag part: 3.1"
    
    }
    You can do other stuff with them. There are member functions for calculating the magnitude, squared magnitude, and phase angle if you want to use them with polar coordinates. There is built in support for you to multiply them, add, divide, etc. with other vectors and scalar values. You can use these complex objects in arguments to the pow, sqrt, sin, cos, tan, sinh, cosh, tanh functions.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Absolute value of imaginary numbers?
    By cpjust in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 08-11-2008, 05:52 PM
  2. Complex Numbers with Visual C++
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2007, 02:42 PM
  3. refencing complex numbers with the Ch compiler
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-14-2006, 01:23 AM
  4. l'Hopital's Rule
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 02-12-2003, 09:18 PM
  5. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM