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

Operator < #32

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions big-int/src/bigint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class largeInt
bool operator!=(int);
bool operator!=(int64_t);

bool operator<(largeInt);
bool operator<(const largeInt& z);
MonarchChakri marked this conversation as resolved.
Show resolved Hide resolved
bool operator<(int);
bool operator<(int64_t);
bool operator<(long long);
Expand All @@ -150,4 +150,4 @@ class largeInt
};
} // namespace libbig

#endif
#endif
42 changes: 42 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,47 @@

namespace libbig
{
bool largeInt:: operator < (const largeInt& z) {
// sign = 1: positive number
// sign = 0: negative number
// flip if its reverse

bool NEGATIVE = 0;
MonarchChakri marked this conversation as resolved.
Show resolved Hide resolved
bool POSITIVE = 1;

if (this->sign == NEGATIVE) {
if (z.sign == POSITIVE){
return true;
} else if (z.sign == NEGATIVE){
if (this->number.length() > z.number.length()){
return true;
}
if (this->number.length() < z.number.length()){
return false;
}
for(int i = 0; i < this->number.length(); i++) {
if (this->number[i] < z.number[i]) {
return false;
}
}
}
} else if (this->sign == POSITIVE) {
if (z.sign == NEGATIVE){
return false;
} else if (z.sign == POSITIVE){
if (this->number.length() < z.number.length()){
return true;
}
if (this->number.length() > z.number.length()){
return false;
}
for(int i = 0; i < this->number.length(); i++) {
if (this->number[i] > z.number[i]) {
return false;
}
}
}
}
return false;
}
} // namespace libbig