docs: Fixed some clangformat issues with the documentation

This commit is contained in:
Tajmeet Singh 2020-06-23 14:32:08 +01:00
parent 1158e626cb
commit f4fa366da5

View File

@ -2,11 +2,12 @@
* Copyright 2020 @author tjgurwara99 * Copyright 2020 @author tjgurwara99
* @file * @file
* *
* A basic implementation of Complex Number field as a class with operators overloaded to accommodate (mathematical) field operations. * A basic implementation of Complex Number field as a class with operators
* overloaded to accommodate (mathematical) field operations.
*/ */
#include <iostream>
#include <cmath> #include <cmath>
#include <iostream>
#include <stdexcept> #include <stdexcept>
/** /**
@ -18,9 +19,10 @@ class Complex {
// The imaginary value of the complex number // The imaginary value of the complex number
double im; double im;
public: public:
/** /**
* Complex Constructor which initialises the complex number which takes two arguments. * Complex Constructor which initialises the complex number which takes two
* arguments.
* @param x The real value of the complex number. * @param x The real value of the complex number.
* @param y The imaginary value of the complex number. * @param y The imaginary value of the complex number.
*/ */
@ -28,34 +30,31 @@ class Complex {
this->re = x; this->re = x;
this->im = y; this->im = y;
} }
/** /**
* Complex Constructor which initialises the complex number with no arguments. * Complex Constructor which initialises the complex number with no
* arguments.
*/ */
Complex() { Complex() { Complex(0.0, 0.0); }
Complex(0.0,0.0);
}
/** /**
* Member function (getter) to access the class' re value. * Member function (getter) to access the class' re value.
*/ */
double real() const { double real() const { return this->re; }
return this->re;
}
/** /**
* Member function (getter) to access the class' im value. * Member function (getter) to access the class' im value.
*/ */
double imag() const { double imag() const { return this->im; }
return this->im;
}
/** /**
* Member function to which gives the absolute value (modulus) of our complex number * Member function to which gives the absolute value (modulus) of our
* @return \f$ \sqrt{z \dot \bar{z} \f$ where \f$ z \f$ is our complex number. * complex number
* @return \f$ \sqrt{z \dot \bar{z}} \f$ where \f$ z \f$ is our complex
* number.
*/ */
double abs() const { double abs() const {
return std::sqrt(this->re*this->re + this->im*this->im); return std::sqrt(this->re * this->re + this->im * this->im);
} }
/** /**
@ -63,7 +62,7 @@ class Complex {
* @param other The other number that is added to the current number. * @param other The other number that is added to the current number.
* @return result current number plus other number * @return result current number plus other number
*/ */
Complex operator+(const Complex& other) { Complex operator+(const Complex &other) {
Complex result(this->re + other.re, this->im + other.im); Complex result(this->re + other.re, this->im + other.im);
return result; return result;
} }
@ -73,7 +72,7 @@ class Complex {
* @param other The other number being subtracted from the current number. * @param other The other number being subtracted from the current number.
* @return result current number subtract other number * @return result current number subtract other number
*/ */
Complex operator-(const Complex& other) { Complex operator-(const Complex &other) {
Complex result(this->re - other.re, this->im - other.im); Complex result(this->re - other.re, this->im - other.im);
return result; return result;
} }
@ -83,15 +82,16 @@ class Complex {
* @param other The other number to multiply the current number to. * @param other The other number to multiply the current number to.
* @return result current number times other number. * @return result current number times other number.
*/ */
Complex operator*(const Complex& other) { Complex operator*(const Complex &other) {
Complex result(this->re * other.re - this->im * other.im, Complex result(this->re * other.re - this->im * other.im,
this->re * other.im + this->im * other.re); this->re * other.im + this->im * other.re);
return result; return result;
} }
/** /**
* Operator overload of the BITWISE NOT which gives us the conjugate of our complex number. * Operator overload of the BITWISE NOT which gives us the conjugate of our
* NOTE: This is overloading the BITWISE operator but its not a BITWISE operation in this definition. * complex number. NOTE: This is overloading the BITWISE operator but its
* not a BITWISE operation in this definition.
* @return result The conjugate of our complex number. * @return result The conjugate of our complex number.
*/ */
Complex operator~() const { Complex operator~() const {
@ -100,18 +100,19 @@ class Complex {
} }
/** /**
* Operator overload to be able to divide two complex numbers. This function would throw an exception if the other number is zero. * Operator overload to be able to divide two complex numbers. This function
* would throw an exception if the other number is zero.
* @param other The other number we divide our number by. * @param other The other number we divide our number by.
* @return result Current number divided by other number. * @return result Current number divided by other number.
*/ */
Complex operator/(const Complex& other) { Complex operator/(const Complex &other) {
Complex result = *this * ~other; Complex result = *this * ~other;
double denominator = other.abs() * other.abs(); double denominator = other.abs() * other.abs();
if (denominator != 0) { if (denominator != 0) {
result = Complex(result.real() / denominator, result.imag() / denominator); result = Complex(result.real() / denominator,
result.imag() / denominator);
return result; return result;
} } else {
else {
throw std::invalid_argument("Undefined Value"); throw std::invalid_argument("Undefined Value");
} }
} }
@ -124,23 +125,24 @@ class Complex {
* @return 'True' If real and imaginary parts of a and b are same * @return 'True' If real and imaginary parts of a and b are same
* @return 'False' Otherwise. * @return 'False' Otherwise.
*/ */
bool operator==(const Complex& a, const Complex& b) { bool operator==(const Complex &a, const Complex &b) {
double del_real = a.real() - b.real(); double del_real = a.real() - b.real();
double del_imag = a.imag() - b.imag(); double del_imag = a.imag() - b.imag();
return ((del_real <= 1e-15 && del_real >= - 1e-15 ) && (del_imag <= 1e-15 && del_imag >= - 1e-15)); return ((del_real <= 1e-15 && del_real >= -1e-15) &&
(del_imag <= 1e-15 && del_imag >= -1e-15));
} }
/** /**
* Overloaded insersion operator to accommodate the printing of our complex number in their standard form. * Overloaded insersion operator to accommodate the printing of our complex
* number in their standard form.
* @param os The console stream * @param os The console stream
* @param num The complex number. * @param num The complex number.
*/ */
std::ostream& operator<<(std::ostream& os, const Complex& num) { std::ostream &operator<<(std::ostream &os, const Complex &num) {
os << num.real(); os << num.real();
if (num.imag() < 0) { if (num.imag() < 0) {
os << " - " << -num.imag(); os << " - " << -num.imag();
} } else {
else {
os << " + " << num.imag(); os << " + " << num.imag();
} }
os << "i"; os << "i";
@ -151,28 +153,32 @@ std::ostream& operator<<(std::ostream& os, const Complex& num) {
* Tests Function * Tests Function
*/ */
void tests() { void tests() {
Complex num1(1,1), num2(1,1); Complex num1(1, 1), num2(1, 1);
// Test for addition // Test for addition
assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't add up \n", assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't "
(num1 + num2) == Complex(2,2))); "add up \n",
(num1 + num2) == Complex(2, 2)));
std::cout << "First test passes." << std::endl; std::cout << "First test passes." << std::endl;
// Test for subtraction // Test for subtraction
assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says otherwise. \n", assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says "
(num1 - num2) == Complex(0,0))); "otherwise. \n",
(num1 - num2) == Complex(0, 0)));
std::cout << "Second test passes." << std::endl; std::cout << "Second test passes." << std::endl;
// Test for multiplication // Test for multiplication
assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says otherwise. \n", assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
(num1 * num2) == Complex(0,2))); "otherwise. \n",
(num1 * num2) == Complex(0, 2)));
std::cout << "Third test passes." << std::endl; std::cout << "Third test passes." << std::endl;
// Test for division // Test for division
assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says otherwise.\n", assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
(num1 / num2) == Complex(1,0))); "otherwise.\n",
(num1 / num2) == Complex(1, 0)));
std::cout << "Fourth test passes." << std::endl; std::cout << "Fourth test passes." << std::endl;
// Test for conjugates // Test for conjugates
assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the program says otherwise.\n", assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the "
~num1 == Complex(1,-1))); "program says otherwise.\n",
~num1 == Complex(1, -1)));
std::cout << "Fifth test passes." << std::endl; std::cout << "Fifth test passes." << std::endl;
} }
/** /**
* Main function * Main function