fix: Taken onboard some suggested changes

This commit is contained in:
Tajmeet Singh 2020-06-23 17:56:53 +01:00
parent 52f918a4ff
commit b9658104cb

View File

@ -12,11 +12,6 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
/**
* Global constant for pi to work with Windows environment
*/
const double pi = 3.14159265358979323846;
/** /**
* Class Complex to represent complex numbers as a field. * Class Complex to represent complex numbers as a field.
*/ */
@ -30,8 +25,8 @@ class Complex {
/** /**
* Complex Constructor which initialises the complex number which takes two * Complex Constructor which initialises the complex number which takes two
* arguments. * arguments.
* @param x The real value of the complex number. * @param x The real value of the complex number (optional).
* @param y The imaginary value of the complex number. * @param y The imaginary value of the complex number (optional).
*/ */
explicit Complex(double x = 0.f, double y = 0.f) : re(x), im(y) { ; } explicit Complex(double x = 0.f, double y = 0.f) : re(x), im(y) { ; }
@ -63,23 +58,9 @@ class Complex {
/** /**
* Member function which gives the argument of our complex number. * Member function which gives the argument of our complex number.
* @return Argument of our Complex number. * @return Argument of our Complex number in radians.
*/ */
double arg() const { double arg() const { return atan2(this->im, this->re); }
if (this->re > 0) {
return std::atan(this->im / this->re);
} else if (this->re < 0 && this->im >= 0) {
return std::atan(this->im / this->re) + pi;
} else if (this->re < 0 && this->im < 0) {
return std::atan(this->im / this->re) - pi;
} else if (this->re == 0 && this->im > 0) {
return pi / 2;
} else if (this->re == 0 && this->im < 0) {
return -pi / 2;
} else {
throw std::invalid_argument("Undefined Value");
}
}
/** /**
* Operator overload to be able to add two complex numbers. * Operator overload to be able to add two complex numbers.
@ -181,41 +162,60 @@ std::ostream &operator<<(std::ostream &os, const Complex &num) {
return os; return os;
} }
/**
* Function to get random numbers to generate our complex numbers for test
*/
double get_rand() { return (std::rand() % 100 - 50) / 100.f; }
/** /**
* Tests Function * Tests Function
*/ */
void tests() { void tests() {
Complex num1(1, 1), num2(1, 1); std::srand(std::time(nullptr));
std::complex<double> cnum1(1, 1), cnum2(1, 1); double x1 = get_rand(), y1 = get_rand(), x2 = get_rand(), y2 = get_rand();
Complex num1(x1, y1), num2(x2, y2);
std::complex<double> cnum1(x1, y1), cnum2(x2, y2);
Complex result;
std::complex<double> expected;
// Test for addition // Test for addition
result = num1 + num2;
expected = cnum1 + cnum2;
assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't " assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't "
"add up \n", "add up \n",
((num1 + num2).real() == (cnum1 + cnum2).real() && (result.real() == expected.real() &&
(num1 + num2).imag() == (cnum1 + cnum2).imag()))); result.imag() == expected.imag())));
std::cout << "First test passes." << std::endl; std::cout << "First test passes." << std::endl;
// Test for subtraction // Test for subtraction
result = num1 - num2;
expected = cnum1 - cnum2;
assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says " assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says "
"otherwise. \n", "otherwise. \n",
((num1 - num2).real() == (cnum1 - cnum2).real() && (result.real() == expected.real() &&
(num1 - num2).imag() == (cnum1 - cnum2).imag()))); result.imag() == expected.imag())));
std::cout << "Second test passes." << std::endl; std::cout << "Second test passes." << std::endl;
// Test for multiplication // Test for multiplication
result = num1 * num2;
expected = cnum1 * cnum2;
assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says " assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
"otherwise. \n", "otherwise. \n",
((num1 * num2).real() == (cnum1 * cnum2).real() && (result.real() == expected.real() &&
(num1 * num2).imag() == (cnum1 * cnum2).imag()))); result.imag() == expected.imag())));
std::cout << "Third test passes." << std::endl; std::cout << "Third test passes." << std::endl;
// Test for division // Test for division
result = num1 / num2;
expected = cnum1 / cnum2;
assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says " assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
"otherwise.\n", "otherwise.\n",
((num1 / num2).real() == (cnum1 / cnum2).real() && (result.real() == expected.real() &&
(num1 / num2).imag() == (cnum1 / cnum2).imag()))); result.imag() == expected.imag())));
std::cout << "Fourth test passes." << std::endl; std::cout << "Fourth test passes." << std::endl;
// Test for conjugates // Test for conjugates
result = ~num1;
expected = std::conj(cnum1);
assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the " assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the "
"program says otherwise.\n", "program says otherwise.\n",
((~num1).real() == std::conj(cnum1).real() && (result.real() == expected.real() &&
(~num1).imag() == std::conj(cnum1).imag()))); result.imag() == expected.imag())));
std::cout << "Fifth test passes.\n"; std::cout << "Fifth test passes.\n";
// Test for Argument of our complex number // Test for Argument of our complex number
assert(((void)"(1 + 1i) has argument PI / 4 but the program differs from " assert(((void)"(1 + 1i) has argument PI / 4 but the program differs from "