-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.cpp
40 lines (31 loc) · 921 Bytes
/
complex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* complex.cpp
*
* Created on: Jan 3, 2021
* Author: d-w-h
*/
#include "complex.hpp"
Complex::Complex() {}
Complex::Complex(double a, double b) {
this->a = a;
this->b = b;
}
Complex Complex::operator+(const Complex& m) {
Complex result_addition(0,0);
result_addition.a = this->a + m.a;
result_addition.b = this->b + m.b;
return result_addition;
}
Complex Complex::operator*(const Complex& m) {
Complex result_multiplication(0,0);
result_multiplication.a = this->a*m.a - this->b*m.b;
result_multiplication.b = this->a*m.b + this->b*m.a;
return result_multiplication;
}
Complex Complex::operator/(const Complex& m) {
Complex result_division(0,0);
double c2_d2 = m.a*m.a + m.b*m.b;
result_division.a = (this->a*m.a + this->b*m.b) / (c2_d2 + 1e-20);
result_division.b = (this->b*m.a - this->a*m.b) / (c2_d2 + 1e-20);
return result_division;
}