diff --git a/big-int/src/operators/equal_to.cpp b/big-int/src/operators/equal_to.cpp index 3f7f493..24e2200 100644 --- a/big-int/src/operators/equal_to.cpp +++ b/big-int/src/operators/equal_to.cpp @@ -71,7 +71,7 @@ bool largeInt::operator==(int z) { return false; } - std:: cout << z << std::endl; + z /= 10; n--; } diff --git a/big-int/src/operators/greater_equal.cpp b/big-int/src/operators/greater_equal.cpp index f5c6018..3aa3111 100644 --- a/big-int/src/operators/greater_equal.cpp +++ b/big-int/src/operators/greater_equal.cpp @@ -32,5 +32,25 @@ namespace libbig { - -} // namespace libbig \ No newline at end of file + /** + * @brief greater equal operator overload for largeInt class. + * + * @param right hand parameter of >= operator + * @return true if *this (object on which this function is + * called upon) is greater or equal else + * false + * + */ + bool largeInt::operator>=(const largeInt &a) + { + /** + * if both > and == conditions are satisfied then >= condition + * is satified too + */ + if(*this>a || *this==a) + { + return true; + } + return false; + } +} // namespace libbig diff --git a/big-int/src/operators/greater_than.cpp b/big-int/src/operators/greater_than.cpp index f5c6018..b1f6ea3 100644 --- a/big-int/src/operators/greater_than.cpp +++ b/big-int/src/operators/greater_than.cpp @@ -32,5 +32,77 @@ namespace libbig { + /** + * This operator returns a bool value which is + * 1 when the > condition is satisfied between the two largeInt object + * 0 otherwise + */ + bool largeInt::operator>(const largeInt &a) + { + /** + * If first number is negative and second number is positive + * then > condition is not satisfied. + * + */ + if(this->sign == NEGATIVE && a.sign == POSITIVE) + { + return false; + } -} // namespace libbig \ No newline at end of file + /** + * If first number is positive and second number is negative + * then > condition is satisfied. + * + */ + if(a.sign == NEGATIVE && this->sign == POSITIVE) + { + return true; + } + + /** + * If both the numbers have same sign + * then comparing their lengths. + * + */ + if(this->number.length() > a.number.length()) + { + /** + * if first number is longer and both are negative then + * the condition is satisfied, but if first number is + * longer and both are positive then the condition is not + * satisfied. + */ + return 0^this->sign; + } + if(this->number.length() < a.number.length()) + { + /** + * if first number is shorter and both are negative then + * the condition is not satisfied, but if first number is + * shorter and both are positive then the condition is + * satisfied. + * + */ + return 1^this->sign; + } + + /** + * if the length of both numbers are same then we compare each + * digit from LTR and the first non equal digit can determine + * whether the condition is satisfied + * + */ + for(int i=0;inumber[i]>a.number[i]) + { + return !(1^this->sign); + } + if(this->number[i]sign); + } + } + return false; + } +} // namespace libbig diff --git a/big-int/src/operators/less_equal.cpp b/big-int/src/operators/less_equal.cpp index f5c6018..9099b52 100644 --- a/big-int/src/operators/less_equal.cpp +++ b/big-int/src/operators/less_equal.cpp @@ -32,5 +32,23 @@ namespace libbig { - -} // namespace libbig \ No newline at end of file + /** + * This operator returns a bool value which is + * 1 when the <= condition is satisfied between the two largeInt objects + * 0 otherwise + * + */ + bool largeInt::operator<=(const largeInt &a) + { + /** + * if both < and == conditions are satisfied then <= condition is + * satified too + * + */ + if(*thissign == NEGATIVE && a.sign == POSITIVE) + { + return true; + } + /** + * If first number is positive and second number is negative + * then < condition is not satisfied. + * + */ + if(a.sign == NEGATIVE && this->sign == POSITIVE) + { + return false; + } + + /** + * If both the numbers have same sign + * then comparing their lengths + * + */ + if(this->number.length() > a.number.length()) + { + /** + * if first number is longer and both are negative then the + * condition is not satisfied, but if first number is longer + * and both are positive then the condition is satisfied. + * + */ + return 1^this->sign; + } + if(this->number.length() < a.number.length()) + { + /** + * if first number is shorter and both are negative then the + * condition is satisfied, but if first number is shorter and + * both are positive then the condition is not satisfied. + * + */ + return 0^this->sign; + } + + /** + * if the length of both numbers are same then we compare each digit + * from LTR and the first non equal digit can determine whether the + * condition is satisfied + * + */ + for(int i=0;inumber[i]sign); + } + if(this->number[i]>a.number[i]) + { + return !(0^this->sign); + } + } + return false; + } } // namespace libbig diff --git a/tests/operators/greater_equal_test.cpp b/tests/operators/greater_equal_test.cpp new file mode 100644 index 0000000..2a18cfd --- /dev/null +++ b/tests/operators/greater_equal_test.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main() +{ + libbig::largeInt a,b; + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range + + for(int i=0;i<100;i++) + { + int i1 = distr(gen); + int i2 = distr(gen); + a = i1; + b = i2; + assert((a>=b) == (i1>=i2)); + } +} \ No newline at end of file diff --git a/tests/operators/greater_than_test.cpp b/tests/operators/greater_than_test.cpp new file mode 100644 index 0000000..4aead69 --- /dev/null +++ b/tests/operators/greater_than_test.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main() +{ + libbig::largeInt a,b; + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range + + for(int i=0;i<100;i++) + { + int i1 = distr(gen); + int i2 = distr(gen); + a = i1; + b = i2; + assert((a>b) == (i1>i2)); + } +} \ No newline at end of file diff --git a/tests/operators/less_equal_test.cpp b/tests/operators/less_equal_test.cpp new file mode 100644 index 0000000..89cff4e --- /dev/null +++ b/tests/operators/less_equal_test.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main() +{ + libbig::largeInt a,b; + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range + + for(int i=0;i<100;i++) + { + int i1 = distr(gen); + int i2 = distr(gen); + a = i1; + b = i2; + assert((a<=b) == (i1<=i2)); + } +} \ No newline at end of file diff --git a/tests/operators/less_than_test.cpp b/tests/operators/less_than_test.cpp new file mode 100644 index 0000000..41b2e97 --- /dev/null +++ b/tests/operators/less_than_test.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main() +{ + libbig::largeInt a,b; + + std::random_device rd; // obtain a random number from hardware + std::mt19937 gen(rd()); // seed the generator + std::uniform_int_distribution<> distr(-2147483648, 2147483647); // define the range + + for(int i=0;i<100;i++) + { + int i1 = distr(gen); + int i2 = distr(gen); + a = i1; + b = i2; + assert((a