(
doublex = 0.f,
doubley = 0.f,
boolis_polar =
false) {
64 double real()
const{
returnthis->re; }
70 double imag()
const{
returnthis->im; }
80 returnstd::sqrt(this->re * this->re + this->im * this->im);
87 double arg()
const{
returnstd::atan2(this->im, this->re); }
96 Complexresult(this->re + other.re, this->im + other.im);
107 Complexresult(this->re - other.re, this->im - other.im);
118 Complexresult(this->re * other.re - this->im * other.im,
119this->re * other.im + this->im * other.re);
131 Complexresult(this->re, -(this->im));
143 Complexresult = *
this* ~other;
146 if(denominator != 0) {
147result =
Complex(result.real() / denominator,
148result.imag() / denominator);
151 throwstd::invalid_argument(
"Undefined Value");
161this->re = other.
real();
162this->im = other.
imag();
176 returna.real() == b.
real() && a.imag() == b.
imag();
187os <<
"("<< num.
real();
188 if(num.
imag() < 0) {
189os <<
" - "<< -num.
imag();
191os <<
" + "<< num.
imag();
201double get_rand() {
return(std::rand() % 100 - 50) / 100.f; }
207std::srand(std::time(
nullptr));
209 Complexnum1(x1, y1), num2(x2, y2);
210std::complex<double> cnum1(x1, y1), cnum2(x2, y2);
212std::complex<double> expected;
214result = num1 + num2;
215expected = cnum1 + cnum2;
216assert(((
void)
"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't " 218(result.real() == expected.real() &&
219result.imag() == expected.imag())));
220std::cout <<
"First test passes."<< std::endl;
222result = num1 - num2;
223expected = cnum1 - cnum2;
224assert(((
void)
"1 + 1i - 1 - 1i is equal to 0 but the program says " 226(result.real() == expected.real() &&
227result.imag() == expected.imag())));
228std::cout <<
"Second test passes."<< std::endl;
230result = num1 * num2;
231expected = cnum1 * cnum2;
232assert(((
void)
"(1 + 1i) * (1 + 1i) is equal to 2i but the program says " 234(result.real() == expected.real() &&
235result.imag() == expected.imag())));
236std::cout <<
"Third test passes."<< std::endl;
238result = num1 / num2;
239expected = cnum1 / cnum2;
240assert(((
void)
"(1 + 1i) / (1 + 1i) is equal to 1 but the program says " 242(result.real() == expected.real() &&
243result.imag() == expected.imag())));
244std::cout <<
"Fourth test passes."<< std::endl;
247expected = std::conj(cnum1);
248assert(((
void)
"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the " 249 "program says otherwise.\n",
250(result.real() == expected.real() &&
251result.imag() == expected.imag())));
252std::cout <<
"Fifth test passes.\n";
254assert(((
void)
"(1 + 1i) has argument PI / 4 but the program differs from " 255 "the std::complex result.\n",
256(num1.arg() == std::arg(cnum1))));
257std::cout <<
"Sixth test passes.\n";
259assert(((
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))));
262std::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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4