Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
complex_numbers.cpp File Reference
#include <cassert>
#include <cmath>
#include <complex>
#include <ctime>
#include <iostream>
#include <stdexcept>
Include dependency graph for complex_numbers.cpp:

Classes

class  Complex
 

Functions

bool operator== (const Complex &a, const Complex &b)
 
std::ostreamoperator<< (std::ostream &os, const Complex &num)
 
double get_rand ()
 
void tests ()
 
int main ()
 

Detailed Description

Author
tjgurwara99

A basic implementation of Complex Number field as a class with operators overloaded to accommodate (mathematical) field operations.

Function Documentation

◆ get_rand()

double get_rand ( )

Function to get random numbers to generate our complex numbers for test

186 { return (std::rand() % 100 - 50) / 100.f; }
Here is the call graph for this function:

◆ main()

int main ( )

Main function

253  {
254  tests();
255  return 0;
256 }
Here is the call graph for this function:

◆ operator<<()

std::ostream& operator<< ( std::ostream os,
const Complex num 
)

Overloaded insersion operator to accommodate the printing of our complex number in their standard form.

Parameters
osThe console stream
numThe complex number.
172  {
173  os << "(" << num.real();
174  if (num.imag() < 0) {
175  os << " - " << -num.imag();
176  } else {
177  os << " + " << num.imag();
178  }
179  os << "i)";
180  return os;
181 }
Here is the call graph for this function:

◆ operator==()

bool operator== ( const Complex a,
const Complex b 
)

Logical Equal overload for our Complex class.

Parameters
aLeft hand side of our expression
bRight hand side of our expression
Returns
'True' If real and imaginary parts of a and b are same
'False' Otherwise.
162  {
163  return a.real() == b.real() && a.imag() == b.imag();
164 }
Here is the call graph for this function:

◆ tests()

void tests ( )

Tests Function

191  {
192  std::srand(std::time(nullptr));
193  double x1 = get_rand(), y1 = get_rand(), x2 = get_rand(), y2 = get_rand();
194  Complex num1(x1, y1), num2(x2, y2);
195  std::complex<double> cnum1(x1, y1), cnum2(x2, y2);
196  Complex result;
197  std::complex<double> expected;
198  // Test for addition
199  result = num1 + num2;
200  expected = cnum1 + cnum2;
201  assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't "
202  "add up \n",
203  (result.real() == expected.real() &&
204  result.imag() == expected.imag())));
205  std::cout << "First test passes." << std::endl;
206  // Test for subtraction
207  result = num1 - num2;
208  expected = cnum1 - cnum2;
209  assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says "
210  "otherwise. \n",
211  (result.real() == expected.real() &&
212  result.imag() == expected.imag())));
213  std::cout << "Second test passes." << std::endl;
214  // Test for multiplication
215  result = num1 * num2;
216  expected = cnum1 * cnum2;
217  assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
218  "otherwise. \n",
219  (result.real() == expected.real() &&
220  result.imag() == expected.imag())));
221  std::cout << "Third test passes." << std::endl;
222  // Test for division
223  result = num1 / num2;
224  expected = cnum1 / cnum2;
225  assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
226  "otherwise.\n",
227  (result.real() == expected.real() &&
228  result.imag() == expected.imag())));
229  std::cout << "Fourth test passes." << std::endl;
230  // Test for conjugates
231  result = ~num1;
232  expected = std::conj(cnum1);
233  assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the "
234  "program says otherwise.\n",
235  (result.real() == expected.real() &&
236  result.imag() == expected.imag())));
237  std::cout << "Fifth test passes.\n";
238  // Test for Argument of our complex number
239  assert(((void)"(1 + 1i) has argument PI / 4 but the program differs from "
240  "the std::complex result.\n",
241  (num1.arg() == std::arg(cnum1))));
242  std::cout << "Sixth test passes.\n";
243  // Test for absolute value of our complex number
244  assert(((void)"(1 + 1i) has absolute value sqrt(2) but the program differs "
245  "from the std::complex result. \n",
246  (num1.abs() == std::abs(cnum1))));
247  std::cout << "Seventh test passes.\n";
248 }
Here is the call graph for this function:
std::srand
T srand(T... args)
Complex
Definition: complex_numbers.cpp:19
get_rand
double get_rand()
Definition: complex_numbers.cpp:186
tests
void tests()
Definition: complex_numbers.cpp:191
Complex::real
double real() const
Definition: complex_numbers.cpp:60
std::cout
std::complex::real
T real(T... args)
std::rand
T rand(T... args)
Complex::imag
double imag() const
Definition: complex_numbers.cpp:65
std::endl
T endl(T... args)
std::complex
STL class.
std::complex::imag
T imag(T... args)
std::time
T time(T... args)