From 45baaf59526cb252e5ce3af4b3ef0628dc589d27 Mon Sep 17 00:00:00 2001 From: Chakradhar Palaparthi Date: Mon, 5 Oct 2020 19:56:56 +0530 Subject: [PATCH 1/5] Operator < --- big-int/src/operators/less_than.cpp | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/big-int/src/operators/less_than.cpp b/big-int/src/operators/less_than.cpp index 48d792b..f7cb0f5 100644 --- a/big-int/src/operators/less_than.cpp +++ b/big-int/src/operators/less_than.cpp @@ -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; + 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 From 9eb4a2dd8038eed6cfa68d364385894d6bb69f69 Mon Sep 17 00:00:00 2001 From: Chakradhar Palaparthi Date: Mon, 5 Oct 2020 20:04:55 +0530 Subject: [PATCH 2/5] Changing call by value to const ref This reduces copying and improves performance --- big-int/src/bigint.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index 9d8f030..6fb897e 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -125,7 +125,7 @@ class largeInt bool operator!=(int); bool operator!=(int64_t); - bool operator<(largeInt); + bool operator<(const largeInt& z); bool operator<(int); bool operator<(int64_t); bool operator<(long long); @@ -150,4 +150,4 @@ class largeInt }; } // namespace libbig -#endif \ No newline at end of file +#endif From 8db524064d85a11b323424aee1eb84e0069d8e84 Mon Sep 17 00:00:00 2001 From: Chakradhar Palaparthi Date: Mon, 5 Oct 2020 23:53:18 +0530 Subject: [PATCH 3/5] Removed argument name --- big-int/src/bigint.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index 6fb897e..0dacd03 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -125,7 +125,7 @@ class largeInt bool operator!=(int); bool operator!=(int64_t); - bool operator<(const largeInt& z); + bool operator<(const largeInt&); bool operator<(int); bool operator<(int64_t); bool operator<(long long); From 30a0dcf1b7bea54547f60e5eb5101a65abf2b7a5 Mon Sep 17 00:00:00 2001 From: Chakradhar Palaparthi Date: Mon, 5 Oct 2020 23:57:28 +0530 Subject: [PATCH 4/5] constexpr bools --- big-int/src/bigint.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index 0dacd03..94fb571 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -33,6 +33,13 @@ namespace libbig { +// sign = 1: positive number +// sign = 0: negative number +// flip if its reverse + +constexpr bool NEGATIVE = 0; +constexpr bool POSITIVE = 1; + class largeInt { private: From 864062286bf20fcbfeed934fe4ea28065d65acbc Mon Sep 17 00:00:00 2001 From: Chakradhar Palaparthi Date: Mon, 5 Oct 2020 23:58:26 +0530 Subject: [PATCH 5/5] Moved bools to hpp and converted to constexpr --- big-int/src/operators/less_than.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/big-int/src/operators/less_than.cpp b/big-int/src/operators/less_than.cpp index f7cb0f5..47b8140 100644 --- a/big-int/src/operators/less_than.cpp +++ b/big-int/src/operators/less_than.cpp @@ -33,13 +33,6 @@ namespace libbig { bool largeInt:: operator < (const largeInt& z) { - // sign = 1: positive number - // sign = 0: negative number - // flip if its reverse - - bool NEGATIVE = 0; - bool POSITIVE = 1; - if (this->sign == NEGATIVE) { if (z.sign == POSITIVE){ return true;