complex_numbers.cpp File Reference

An implementation of Complex Number as Objects. More...

`#include <cassert>`
`#include <cmath>`
`#include <complex>`
`#include <ctime>`
`#include <iostream>`
`#include <stdexcept>`
Include dependency graph for complex_numbers.cpp:

## Classes

class | Complex
| Class Complex to represent complex numbers as a field. More...

## Functions

bool | operator== (const Complex &a, const Complex &b)
| Operator overload of '==' on Complex class. Logical Equal overload for our Complex class. More...
std::ostream & | operator<< (std::ostream &os, const Complex &num)
| Operator overload of '<<' of ostream for Complex class. Overloaded insersion operator to accommodate the printing of our complex number in their standard form. More...
double | get_rand ()
| Function to get random numbers to generate our complex numbers for test.
void | tests ()
int | main ()

## Detailed Description

An implementation of Complex Number as Objects.

Author
  tjgurwara99

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

## Function Documentation

## main()

int main | ( | void |  | ) |

Main function

{
tests();
return 0;
}
tests
void tests()
Definition: complex_numbers.cpp:206
Here is the call graph for this function:

## operator<<()

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

Operator overload of '<<' of ostream for Complex class. Overloaded insersion operator to accommodate the printing of our complex number in their standard form.

Parameters

  os | The console stream
  num | The complex number.

{
os << "(" << num.real();
if (num.imag() < 0) {
os << " - " << -num.imag();
} else {
os << " + " << num.imag();
}
os << "i)";
return os;
}
Complex::real
double real() const
Member function to get real value of our complex number. Member function (getter) to access the class...
Definition: complex_numbers.cpp:64
Complex::imag
double imag() const
Member function to get imaginary value of our complex number. Member function (getter) to access the ...
Definition: complex_numbers.cpp:70
Here is the call graph for this function:

## operator==()

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

Operator overload of '==' on Complex class. Logical Equal overload for our Complex class.

Parameters

  a | Left hand side of our expression
  b | Right hand side of our expression

Returns
  'True' If real and imaginary parts of a and b are same
  'False' Otherwise.

{
return a.real() == b.real() && a.imag() == b.imag();
}
Here is the call graph for this function:

## tests()

void tests | ( |  | ) |

Tests Function

{
std::srand(std::time(nullptr));
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
result = num1 + num2;
expected = cnum1 + cnum2;
assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't "
"add up \n",
(result.real() == expected.real() &&
result.imag() == expected.imag())));
std::cout << "First test passes." << std::endl;
// Test for subtraction
result = num1 - num2;
expected = cnum1 - cnum2;
assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says "
"otherwise. \n",
(result.real() == expected.real() &&
result.imag() == expected.imag())));
std::cout << "Second test passes." << std::endl;
// Test for multiplication
result = num1 * num2;
expected = cnum1 * cnum2;
assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
"otherwise. \n",
(result.real() == expected.real() &&
result.imag() == expected.imag())));
std::cout << "Third test passes." << std::endl;
// Test for division
result = num1 / num2;
expected = cnum1 / cnum2;
assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
"otherwise.\n",
(result.real() == expected.real() &&
result.imag() == expected.imag())));
std::cout << "Fourth test passes." << std::endl;
// Test for conjugates
result = ~num1;
expected = std::conj(cnum1);
assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the "
"program says otherwise.\n",
(result.real() == expected.real() &&
result.imag() == expected.imag())));
std::cout << "Fifth test passes.\n";
// Test for Argument of our complex number
assert(((void)"(1 + 1i) has argument PI / 4 but the program differs from "
"the std::complex result.\n",
(num1.arg() == std::arg(cnum1))));
std::cout << "Sixth test passes.\n";
// Test for absolute value of our complex number
assert(((void)"(1 + 1i) has absolute value sqrt(2) but the program differs "
"from the std::complex result. \n",
(num1.abs() == std::abs(cnum1))));
std::cout << "Seventh test passes.\n";
}
std::cout
Complex
Class Complex to represent complex numbers as a field.
Definition: complex_numbers.cpp:20
get_rand
double get_rand()
Function to get random numbers to generate our complex numbers for test.
Definition: complex_numbers.cpp:201
std::complex
std::endl
T endl(T... args)
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::complex::imag
T imag(T... args)
std::complex::real
T real(T... args)
std::srand
T srand(T... args)
std::time
T time(T... args)
Here is the call graph for this function: