Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added >, <, <=, >= operators along with tests #48

Merged
merged 7 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion big-int/src/operators/equal_to.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bool largeInt::operator==(int z)
{
return false;
}
std:: cout << z << std::endl;

z /= 10;
n--;
}
Expand Down
24 changes: 22 additions & 2 deletions big-int/src/operators/greater_equal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,25 @@

namespace libbig
{

} // namespace libbig
/**
* @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
74 changes: 73 additions & 1 deletion big-int/src/operators/greater_than.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
* 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;i<a.number.length();i++)
{
if(this->number[i]>a.number[i])
{
return !(1^this->sign);
}
if(this->number[i]<a.number[i])
{
return !(0^this->sign);
}
}
return false;
}
} // namespace libbig
22 changes: 20 additions & 2 deletions big-int/src/operators/less_equal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,23 @@

namespace libbig
{

} // namespace libbig
/**
* 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(*this<a || *this==a)
{
return true;
}
return false;
}
} // namespace libbig
72 changes: 72 additions & 0 deletions big-int/src/operators/less_than.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,77 @@

namespace libbig
{
/**
* 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 first number is negative and second number is positive
* then < condition is satisfied.
*
*/
if(this->sign == 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;i<a.number.length();i++)
{
if(this->number[i]<a.number[i])
{
return !(1^this->sign);
}
if(this->number[i]>a.number[i])
{
return !(0^this->sign);
}
}
return false;
}
} // namespace libbig
22 changes: 22 additions & 0 deletions tests/operators/greater_equal_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
#include<cassert>
#include<random>
#include<bigint.hpp>

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));
}
}
22 changes: 22 additions & 0 deletions tests/operators/greater_than_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
#include<cassert>
#include<random>
#include<bigint.hpp>

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));
}
}
22 changes: 22 additions & 0 deletions tests/operators/less_equal_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
#include<cassert>
#include<random>
#include<bigint.hpp>

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));
}
}
22 changes: 22 additions & 0 deletions tests/operators/less_than_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
#include<cassert>
#include<random>
#include<bigint.hpp>

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));
}
}