TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
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:

Go to the source code of this file.

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.
 
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.
 
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.

Definition in file complex_numbers.cpp.

Function Documentation

◆ get_rand()

double get_rand ( )

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

Definition at line 201 of file complex_numbers.cpp.

201{ return (std::rand() % 100 - 50) / 100.f; }

◆ main()

int main ( void )

Main function

Definition at line 268 of file complex_numbers.cpp.

268 {
269 tests();
270 return 0;
271}
void tests()

◆ 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
osThe console stream
numThe complex number.

Definition at line 186 of file complex_numbers.cpp.

186 {
187 os << "(" << num.real();
188 if (num.imag() < 0) {
189 os << " - " << -num.imag();
190 } else {
191 os << " + " << num.imag();
192 }
193 os << "i)";
194 return os;
195}
double real() const
Member function to get real value of our complex number. Member function (getter) to access the class...
double imag() const
Member function to get imaginary value of our complex number. Member function (getter) to access the ...

◆ operator==()

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

Operator overload of '==' on Complex class. 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.

Definition at line 175 of file complex_numbers.cpp.

175 {
176 return a.real() == b.real() && a.imag() == b.imag();
177}

◆ tests()

void tests ( )

Tests Function

Definition at line 206 of file complex_numbers.cpp.

206 {
207 std::srand(std::time(nullptr));
208 double x1 = get_rand(), y1 = get_rand(), x2 = get_rand(), y2 = get_rand();
209 Complex num1(x1, y1), num2(x2, y2);
210 std::complex<double> cnum1(x1, y1), cnum2(x2, y2);
212 std::complex<double> expected;
213 // Test for addition
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 "
217 "add up \n",
218 (result.real() == expected.real() &&
219 result.imag() == expected.imag())));
220 std::cout << "First test passes." << std::endl;
221 // Test for subtraction
222 result = num1 - num2;
223 expected = cnum1 - cnum2;
224 assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says "
225 "otherwise. \n",
226 (result.real() == expected.real() &&
227 result.imag() == expected.imag())));
228 std::cout << "Second test passes." << std::endl;
229 // Test for multiplication
230 result = num1 * num2;
231 expected = cnum1 * cnum2;
232 assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
233 "otherwise. \n",
234 (result.real() == expected.real() &&
235 result.imag() == expected.imag())));
236 std::cout << "Third test passes." << std::endl;
237 // Test for division
238 result = num1 / num2;
239 expected = cnum1 / cnum2;
240 assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
241 "otherwise.\n",
242 (result.real() == expected.real() &&
243 result.imag() == expected.imag())));
244 std::cout << "Fourth test passes." << std::endl;
245 // Test for conjugates
246 result = ~num1;
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";
253 // Test for Argument of our complex number
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";
258 // Test for absolute value of our complex number
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";
263}
Class Complex to represent complex numbers as a field.
double get_rand()
Function to get random numbers to generate our complex numbers for test.
uint64_t result(uint64_t n)