43 explicit Complex(
double x = 0.f,
double y = 0.f,
bool is_polar =
false) {
64 double real()
const {
return this->re; }
70 double imag()
const {
return this->im; }
80 return std::sqrt(this->re * this->re + this->im * this->im);
87 double arg()
const {
return std::atan2(this->im, this->re); }
96 Complex result(this->re + other.re, this->im + other.im);
107 Complex result(this->re - other.re, this->im - other.im);
118 Complex result(this->re * other.re - this->im * other.im,
119 this->re * other.im + this->im * other.re);
131 Complex result(this->re, -(this->im));
143 Complex result = *
this * ~other;
146 if (denominator != 0) {
147 result =
Complex(result.real() / denominator,
148 result.imag() / denominator);
151 throw std::invalid_argument(
"Undefined Value");
161 this->re = other.
real();
162 this->im = other.
imag();
176 return a.real() == b.
real() && a.imag() == b.
imag();
187 os <<
"(" << num.
real();
188 if (num.
imag() < 0) {
189 os <<
" - " << -num.
imag();
191 os <<
" + " << num.
imag();
201double get_rand() {
return (std::rand() % 100 - 50) / 100.f; }
207 std::srand(std::time(
nullptr));
209 Complex num1(x1, y1), num2(x2, y2);
210 std::complex<double> cnum1(x1, y1), cnum2(x2, y2);
212 std::complex<double> expected;
214 result = num1 + num2;
215 expected = cnum1 + cnum2;
216 assert(((
void)
"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't "
218 (result.real() == expected.real() &&
219 result.imag() == expected.imag())));
220 std::cout <<
"First test passes." << std::endl;
222 result = num1 - num2;
223 expected = cnum1 - cnum2;
224 assert(((
void)
"1 + 1i - 1 - 1i is equal to 0 but the program says "
226 (result.real() == expected.real() &&
227 result.imag() == expected.imag())));
228 std::cout <<
"Second test passes." << std::endl;
230 result = num1 * num2;
231 expected = cnum1 * cnum2;
232 assert(((
void)
"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
234 (result.real() == expected.real() &&
235 result.imag() == expected.imag())));
236 std::cout <<
"Third test passes." << std::endl;
238 result = num1 / num2;
239 expected = cnum1 / cnum2;
240 assert(((
void)
"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
242 (result.real() == expected.real() &&
243 result.imag() == expected.imag())));
244 std::cout <<
"Fourth test passes." << std::endl;
247 expected = std::conj(cnum1);
248 assert(((
void)
"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the "
249 "program says otherwise.\n",
250 (result.real() == expected.real() &&
251 result.imag() == expected.imag())));
252 std::cout <<
"Fifth test passes.\n";
254 assert(((
void)
"(1 + 1i) has argument PI / 4 but the program differs from "
255 "the std::complex result.\n",
256 (num1.arg() == std::arg(cnum1))));
257 std::cout <<
"Sixth test passes.\n";
259 assert(((
void)
"(1 + 1i) has absolute value sqrt(2) but the program differs "
260 "from the std::complex result. \n",
261 (num1.abs() == std::abs(cnum1))));
262 std::cout <<
"Seventh test passes.\n";
Class Complex to represent complex numbers as a field.
double real() const
Member function to get real value of our complex number. Member function (getter) to access the class...
Complex operator-(const Complex &other)
Operator overload of '-' on Complex class. Operator overload to be able to subtract two complex numbe...
Complex(double x=0.f, double y=0.f, bool is_polar=false)
Complex Constructor which initialises our complex number.
Complex(const Complex &other)
Copy Constructor.
const Complex & operator=(const Complex &other)
Operator overload of '=' on Complex class. Operator overload to be able to copy RHS instance of Compl...
Complex operator+(const Complex &other)
Operator overload of '+' on Complex class. Operator overload to be able to add two complex numbers.
Complex operator~() const
Operator overload of '~' on Complex class. Operator overload of the BITWISE NOT which gives us the co...
Complex operator*(const Complex &other)
Operator overload of '*' on Complex class. Operator overload to be able to multiple two complex numbe...
Complex operator/(const Complex &other)
Operator overload of '/' on Complex class. Operator overload to be able to divide two complex numbers...
double arg() const
Member function to give the argument of our complex number.
double abs() const
Member function to give the modulus of our complex number. Member function to which gives the absolut...
double imag() const
Member function to get imaginary value of our complex number. Member function (getter) to access the ...
std::ostream & operator<<(std::ostream &os, const Complex &num)
Operator overload of '<<' of ostream for Complex class. Overloaded insersion operator to accommodate ...
bool operator==(const Complex &a, const Complex &b)
Operator overload of '==' on Complex class. Logical Equal overload for our Complex class.
double get_rand()
Function to get random numbers to generate our complex numbers for test.