Results 1 to 6 of 6

Thread: Complex Numbers Problem

  1. #1
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Default Complex Numbers Problem

    I just googled myself for an hour and didn't find any usefull information, please help me out

    I want to define 2 complex numbers and add them ... I found things like
    Qt Code:
    1. complex<double>
    To copy to clipboard, switch view to plain text mode 
    and stuff. But I dont get it to work correctly. I also tried including the <complex> library, but still no sucsess...

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Complex Numbers Problem

    Show your test program's code and the error you are getting.

    No one here is a psychic.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Default Re: Complex Numbers Problem

    Oh I wasnt even that "far" but I will try:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPoint>
    4. #include <QtCore>
    5. #include <complex>
    6.  
    7.  
    8. complex<double> a = -0.2i;
    9. complex<double> b = 0.4i;
    10.  
    11. complex<double> c = a+b;
    12.  
    13. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    14. ui->setupUi(this);
    15. }
    16.  
    17. MainWindow::~MainWindow() {
    18. delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 
    This already fails at "complex<double> a ..." with "complex does not name a type". I was not able to find a working example for just adding two complex numbers

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Complex Numbers Problem

    complex is a part of std namespace
    Qt Code:
    1. using namespace std;
    2.  
    3. // or
    4.  
    5. std::complex<double> i;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Complex Numbers Problem

    You will also need to construct the complex number objects correctly.
    Qt Code:
    1. std::complex<double> a(0, -0.2);
    2. std::complex<double> b(0, 0.4);
    3.  
    4. std::complex<double> result = a + b;
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Complex Numbers Problem

    Qt Code:
    1. complex<double> a = -0.2i;
    2. complex<double> b = 0.4i;
    To copy to clipboard, switch view to plain text mode 
    This isn't defined in C++. The compiler cannot compile "0.2i". As mentioned above, "i" is a predefined complex constant. Therefore:
    Qt Code:
    1. complex<double> a = -0.2*i;
    2. complex<double> b = 0.4*i;
    To copy to clipboard, switch view to plain text mode 
    Naturally, you can use "i" in your computations, for example:
    Qt Code:
    1. complex<double> c = i*(a + b) - 2*i;
    2. complex<double> d = 5 + 6*i;
    To copy to clipboard, switch view to plain text mode 
    The complex has a double() operator as well, you can:
    Qt Code:
    1. complex<double> e = 6.6;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. A simple problem with complex solutions re rs 232
    By Cedarviola in forum Qt Programming
    Replies: 1
    Last Post: 9th October 2013, 07:02
  2. Replies: 2
    Last Post: 17th October 2010, 12:16
  3. Replies: 1
    Last Post: 6th July 2010, 15:40
  4. Replies: 1
    Last Post: 7th December 2009, 14:59
  5. Complex Numbers
    By Max Yaffe in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2007, 17:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.