View Full Version : Complex Numbers Problem
xtlc
16th January 2014, 10:55
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
complex<double> and stuff. But I dont get it to work correctly. I also tried including the <complex> library, but still no sucsess...
anda_skoa
16th January 2014, 11:49
Show your test program's code and the error you are getting.
No one here is a psychic.
Cheers,
_
xtlc
16th January 2014, 14:30
Oh I wasnt even that "far" :) but I will try:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPoint>
#include <QtCore>
#include <complex>
complex<double> a = -0.2i;
complex<double> b = 0.4i;
complex<double> c = a+b;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
}
MainWindow::~MainWindow() {
delete ui;
}
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 :(
stampede
16th January 2014, 14:50
complex is a part of std namespace
using namespace std;
// or
std::complex<double> i;
ChrisW67
16th January 2014, 23:43
You will also need to construct the complex number objects correctly.
std::complex<double> a(0, -0.2);
std::complex<double> b(0, 0.4);
std::complex<double> result = a + b;
Radek
17th January 2014, 15:34
complex<double> a = -0.2i;
complex<double> b = 0.4i;
This isn't defined in C++. The compiler cannot compile "0.2i". As mentioned above, "i" is a predefined complex constant. Therefore:
complex<double> a = -0.2*i;
complex<double> b = 0.4*i;
Naturally, you can use "i" in your computations, for example:
complex<double> c = i*(a + b) - 2*i;
complex<double> d = 5 + 6*i;
The complex has a double() operator as well, you can:
complex<double> e = 6.6;
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.