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 5 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
8 changes: 4 additions & 4 deletions big-int/src/bigint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ class largeInt
bool operator!=(int);
bool operator!=(int64_t);

bool operator<(largeInt);
bool operator<(const largeInt &);
bool operator<(int);
bool operator<(int64_t);

bool operator>(largeInt);
bool operator>(const largeInt &);
bool operator>(int);
bool operator>(int64_t);

bool operator<=(largeInt);
bool operator<=(const largeInt &);
bool operator<=(int);
bool operator<=(int64_t);

bool operator>=(largeInt);
bool operator>=(const largeInt &);
bool operator>=(int);
bool operator>=(int64_t);

Expand Down
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
15 changes: 14 additions & 1 deletion big-int/src/operators/greater_equal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,18 @@

namespace libbig
{

//! Operator overload for >= for largeInt class
/*!
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(*this>a || *this==a) //! if both > and == conditions are satisfied then >= condition is satified too
{
return 1;
}
return 0;
}
} // namespace libbig
48 changes: 47 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,51 @@

namespace libbig
{
//! Operator overload for > for largeInt class
/*!
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 not satisfied.
*/
if(this->sign == NEGATIVE && a.sign == POSITIVE)
{
return 0;
}
/** If first number is positive and second number is negative
* then > condition is satisfied.
*/
if(a.sign == NEGATIVE && this->sign == POSITIVE)
{
return 1;
}
/** If both the numbers have same sign
* then comparing their lengths
*/
if(this->number.length() > a.number.length())
{
return 0^this->sign; //! 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.
if(this->number.length() < a.number.length())
{
return 1^this->sign; //! 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.

} // namespace libbig
for(int i=0;i<a.number.length();i++) //! 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
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
{
if(this->number[i]>a.number[i])
{
return !(1^this->sign);
}
if(this->number[i]<a.number[i])
{
return !(0^this->sign);
}
}
return 0;
}
} // namespace libbig
15 changes: 14 additions & 1 deletion big-int/src/operators/less_equal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,18 @@

namespace libbig
{

//! Operator overload for <= for largeInt class
/*!
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(*this<a || *this==a) //! if both < and == conditions are satisfied then <= condition is satified too
{
return 1;
}
return 0;
}
} // namespace libbig
46 changes: 46 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,51 @@

namespace libbig
{
//! Operator overload for < for largeInt class
/*!
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 1;
}
/** If first number is positive and second number is negative
* then < condition is not satisfied.
*/
if(a.sign == NEGATIVE && this->sign == POSITIVE)
{
return 0;
}
/** If both the numbers have same sign
* then comparing their lengths
*/
if(this->number.length() > a.number.length())
{
return 1^this->sign; //! 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.
if(this->number.length() < a.number.length())
{
return 0^this->sign; //! 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.

for(int i=0;i<a.number.length();i++) //! 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
{
if(this->number[i]<a.number[i])
{
return !(1^this->sign);
}
if(this->number[i]>a.number[i])
{
return !(0^this->sign);
}
}
return 0;
}
} // 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));
}
}