From 859d24be9c0f63c047278e7e69df779931a42206 Mon Sep 17 00:00:00 2001 From: tigertv Date: Wed, 9 Dec 2020 16:12:31 +0200 Subject: [PATCH 01/19] Update travis config - minus 2 machines to finish the workflow faster --- .travis.yml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index f48d3b8..532fc17 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,22 +1,10 @@ language: cpp compiler: - - g++ - clang++ os: - linux jobs: include: - # works on Precise and Trusty - - os: linux - addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - g++-5 - env: - - MATRIX_EVAL="CC=gcc-5 && CXX=g++-5" - # works on Precise and Trusty - os: linux addons: From 0741615f2b0231bf2f83764eb4497c27555fe900 Mon Sep 17 00:00:00 2001 From: tigertv Date: Thu, 10 Dec 2020 20:53:01 +0200 Subject: [PATCH 02/19] Add rational numbers --- CMakeLists.txt | 3 +- include/Bint/Brat.h | 118 ++++++++++++++++++++++++ src/Brat.cpp | 58 ++++++++++++ src/Brat/arithmetics.cpp | 194 +++++++++++++++++++++++++++++++++++++++ src/Brat/comparison.cpp | 144 +++++++++++++++++++++++++++++ src/Brat/io.cpp | 134 +++++++++++++++++++++++++++ tests/brat.cpp | 59 ++++++++++++ 7 files changed, 709 insertions(+), 1 deletion(-) create mode 100644 include/Bint/Brat.h create mode 100644 src/Brat.cpp create mode 100644 src/Brat/arithmetics.cpp create mode 100644 src/Brat/comparison.cpp create mode 100644 src/Brat/io.cpp create mode 100644 tests/brat.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 19f02de..fe0dba7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,8 @@ endif() # sources include_directories(include) aux_source_directory(src/Bint BINT_SRC) -set(BINT_SRC ${BINT_SRC} src/Bint.cpp) +aux_source_directory(src/Brat BRAT_SRC) +set(BINT_SRC ${BINT_SRC} ${BRAT_SRC} src/Bint.cpp src/Brat.cpp) set(MATH_SRC src/Math.cpp) add_library(shareobjects OBJECT ${BINT_SRC} ${MATH_SRC}) diff --git a/include/Bint/Brat.h b/include/Bint/Brat.h new file mode 100644 index 0000000..a73d098 --- /dev/null +++ b/include/Bint/Brat.h @@ -0,0 +1,118 @@ +/* + * MIT License + * + * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * Copyright (c) 2020 Max Vetrov(tigertv). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef TIGERTV_BIG_RATIONAL_H +#define TIGERTV_BIG_RATIONAL_H + +#include +#include "Bint.h" + + +namespace TigerTV { + +class Brat { +private: + Bint numerator; + Bint denominator; + + void simplify(); + +public: + // Constructors + //Brat(); + Brat(const Bint& numerator, const Bint& denominator); + + // String representation + std::string toString() const; + std::string base(const uint64_t base) const; + std::string point(const uint64_t num) const; + operator std::string() const; + + // Arithmetics + Brat operator ++ (int); // postfix + Brat& operator ++ (); // prefix + Brat operator -- (int); // postfix + Brat& operator -- (); // prefix + Brat operator - () const; // prefix + Brat operator + (const Brat& a) const; + Brat& operator += (const Brat& a); + Brat operator - (const Brat& a) const; + Brat& operator -= (const Brat& a); + Brat operator * (const Brat& a) const; + Brat& operator *= (const Brat& a); + Brat operator / (const Brat& a) const; + Brat& operator /= (const Brat& a); + + // Comparison + bool operator <= (const Brat &b) const; + bool operator >= (const Brat &b) const; + bool operator == (const Brat &b) const; + bool operator != (const Brat &b) const; + bool operator < (const Brat &b) const; + bool operator > (const Brat &b) const; +/* + Bint operator + (const int64_t a) const; + Bint& operator += (const int64_t a); + Bint operator - (const int64_t a) const; + Bint& operator -= (const int64_t a); + Bint operator * (const int64_t a) const; + Bint& operator *= (const int64_t a); + Bint operator / (const int64_t a) const; + Bint& operator /= (const int64_t a); + + + bool operator <= (const int64_t a) const; + bool operator >= (const int64_t a) const; + bool operator == (const int64_t a) const; + bool operator != (const int64_t a) const; + bool operator < (const int64_t a) const; + bool operator > (const int64_t a) const; + //*/ +}; + +// Input-output +std::ostream& operator << (std::ostream& strm, const Brat& a); +/* +std::istream& operator >> (std::istream& strm, Brat& a); +//*/ +// Left side operators for int +// Arithmetics +Brat operator + (const int64_t a, const Brat& b); +Brat operator - (const int64_t a, const Brat& b); +Brat operator * (const int64_t a, const Brat& b); +Brat operator / (const int64_t a, const Brat& b); +/* +// Comparison +bool operator <= (const int64_t a, const Bint &b); +bool operator >= (const int64_t a, const Bint &b); +bool operator == (const int64_t a, const Bint &b); +bool operator != (const int64_t a, const Bint &b); +bool operator < (const int64_t a, const Bint &b); +bool operator > (const int64_t a, const Bint &b); +//*/ + +} // namespace + +#endif diff --git a/src/Brat.cpp b/src/Brat.cpp new file mode 100644 index 0000000..5800e78 --- /dev/null +++ b/src/Brat.cpp @@ -0,0 +1,58 @@ +/* + * MIT License + * + * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * Copyright (c) 2020 Max Vetrov(tigertv). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include + + +namespace TigerTV { + +Brat::Brat(const Bint& numerator, const Bint& denominator) : numerator(numerator), denominator(denominator) { + simplify(); +} + +void Brat::simplify() { + Bint c = Math::gcd(numerator, denominator); + if (c != 1) { + numerator /= c; + denominator /= c; + } +} + +/* +Bint::Bint() { + number.push_back(0); +} + +Bint::Bint(const std::string& s) : Bint() { + +} + +Bint::Bint(const char* decimal) : Bint((std::string)decimal) { + +} +//*/ + +} // namespace diff --git a/src/Brat/arithmetics.cpp b/src/Brat/arithmetics.cpp new file mode 100644 index 0000000..eb276ad --- /dev/null +++ b/src/Brat/arithmetics.cpp @@ -0,0 +1,194 @@ +/* + * MIT License + * + * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * Copyright (c) 2020 Max Vetrov(tigertv). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + + +namespace TigerTV { + +Brat Brat::operator + (const Brat& a) const { + Brat b = *this; + b += a; + return b; +} + +Brat& Brat::operator += (const Brat& a) { + numerator *= a.denominator; + numerator += denominator * a.numerator; + denominator *= a.denominator; + simplify(); + return *this; +} + +/* +Bint Bint::operator + (const int64_t a) const { + Bint b = *this; + b += a; + return b; +} + +Bint& Bint::operator += (const int64_t a) { + +} +//*/ + +Brat Brat::operator - (const Brat& a) const { + Brat b(*this); + b -= a; + return b; +} + +Brat& Brat::operator -= (const Brat& a) { + Brat b(a); + b.numerator = - b.numerator; + (*this) += b; + return *this; +} + +Brat Brat::operator * (const Brat& a) const { + Brat b(*this); + b *= a; + return b; +} + +Brat& Brat::operator *= (const Brat& a) { + numerator *= a.numerator; + denominator *= a.denominator; + simplify(); + return *this; +} + +Brat Brat::operator / (const Brat& a) const { + Brat b(*this); + b /= a; + return b; +} + +Brat& Brat::operator /= (const Brat& a) { + numerator *= a.denominator; + denominator *= a.numerator; + simplify(); + return *this; +} + +Brat& Brat::operator ++ () { // prefix + numerator += denominator; + return *this; +} + +Brat Brat::operator ++ (int) { // postfix + Brat b = (*this); + ++(*this); + return b; +} + + +Brat& Brat::operator -- () { // prefix + numerator -= denominator; + return *this; +} + +Brat Brat::operator -- (int) { // postfix + Brat b(*this); + --(*this); + return b; +} + +Brat Brat::operator - () const { // prefix + Brat a(*this); + a.numerator = -a.numerator; + return a; +} + +/* +Bint Bint::operator - (const int64_t a) const { + Bint b(*this); + b -= a; + return b; +} + +Bint& Bint::operator -= (const int64_t a) { + *this += -a; + return *this; +} + +Bint Bint::operator * (const int64_t a) const { + Bint b = *this; + b *= a; + return b; +} + +Bint& Bint::operator *= (const int64_t a) { + +} + +Bint Bint::operator / (const int64_t a) const { + Bint b(*this); + b /= a; + return b; +} + +Bint& Bint::operator /= (const int64_t a) { + Bint c(*this); + Bint res; + + div(c, res, a); + + this->number = res.number; + return (*this); +} + +//////////////////////////////////////////////////////////////////////// + +//*/ +/////////////////////////////////////////////////////////////// +// Left side operators for int +/////////////////////////////////////////////////////////////// + +Brat operator + (const int64_t a, const Brat& b) { + Brat c(a, 1); + c += b; + return c; +} + +Brat operator - (const int64_t a, const Brat& b) { + Brat d(a, 1); + d -= b; + return d; +} + +Brat operator * (const int64_t a, const Brat& b) { + Brat c(a, 1); + c *= b; + return c; +} + +Brat operator / (const int64_t a, const Brat& b) { + Brat c(a, 1); + c /= b; + return c; +} + +} // namespace diff --git a/src/Brat/comparison.cpp b/src/Brat/comparison.cpp new file mode 100644 index 0000000..0fa4dec --- /dev/null +++ b/src/Brat/comparison.cpp @@ -0,0 +1,144 @@ +/* + * MIT License + * + * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * Copyright (c) 2020 Max Vetrov(tigertv). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + + +namespace TigerTV { + +bool Brat::operator == (const Brat& b) const { + return (numerator == b.numerator && denominator == b.denominator); +} + +bool Brat::operator != (const Brat& b) const { + return (numerator != b.numerator || denominator != b.denominator); +} + +bool Brat::operator > (const Brat& b) const { + Brat a(*this); + Brat bb(b); + + a.numerator *= bb.denominator; + bb.numerator *= a.denominator; + a.denominator *= bb.denominator; + bb.denominator = a.denominator; + + if (a.denominator > 0) { + return (a.numerator > bb.numerator); + } + + return (a.numerator < bb.numerator); +} + +bool Brat::operator < (const Brat& b) const { + Brat a(*this); + Brat bb(b); + + a.numerator *= bb.denominator; + bb.numerator *= a.denominator; + a.denominator *= bb.denominator; + bb.denominator = a.denominator; + + if (a.denominator > 0) { + return (a.numerator < bb.numerator); + } + + return (a.numerator > bb.numerator); +} + +bool Brat::operator <= (const Brat& b) const { + return !(*this > b); +} + +bool Brat::operator >= (const Brat& b) const { + return !(*this < b); +} + +/* +bool Bint::operator == (const int64_t a) const { + if (number.size() != 1) return false; + return (number[0] == (uint64_t)a); +} + +bool Bint::operator != (const int64_t a) const { + if (number.size() != 1) return true; + return (number[0] != (uint64_t)a); +} + +bool Bint::operator > (const int64_t a) const { + bool neg = isNegative(); + bool negA = (a < 0); + if (neg != negA) return negA; + if (number.size() > 1) return !neg; + return (number[0] > (uint64_t)a); +} + +bool Bint::operator < (const int64_t a) const { + bool neg = isNegative(); + bool negA = (a < 0); + if (neg != negA) return neg; + if (number.size() > 1) return neg; + return (number[0] < (uint64_t)a); +} + + +bool Bint::operator <= (const int64_t a) const { + return !(*this > a); +} + +bool Bint::operator >= (const int64_t a) const { + return !(*this < a); +} + +/////////////////////////////////////////////////////////////// +// Left side operators for int +/////////////////////////////////////////////////////////////// + +bool operator <= (const int64_t a, const Bint &b) { + return b < a; +} + +bool operator >= (const int64_t a, const Bint &b) { + return b > a; +} + +bool operator == (const int64_t a, const Bint &b) { + return b == a; +} + +bool operator != (const int64_t a, const Bint &b) { + return b != a; +} + +bool operator < (const int64_t a, const Bint &b) { + return b > a; +} + +bool operator > (const int64_t a, const Bint &b) { + return b < a; +} +//*/ + +} // namespace diff --git a/src/Brat/io.cpp b/src/Brat/io.cpp new file mode 100644 index 0000000..399f21f --- /dev/null +++ b/src/Brat/io.cpp @@ -0,0 +1,134 @@ +/* + * MIT License + * + * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * Copyright (c) 2020 Max Vetrov(tigertv). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +//#include +#include + +namespace TigerTV { + +Brat::operator std::string() const { + return this->toString(); +} + +std::string Brat::toString() const { + return base(10); +} + +std::string Brat::base(const uint64_t base) const { + return numerator.base(base) + '/' + denominator.base(base); +} + +std::string Brat::point(const uint64_t num) const { + int base = 10; + Bint a = numerator; + Bint t = a / denominator; + a %= denominator; + std::string s = t.toString() + '.'; + std::string temp; + uint64_t i; + + // get zeros + for (i = num; i != 0; --i) { + a *= base; + t = a / denominator; + a %= denominator; + if (t != 0) break; + s += '0'; // wrong for alphabet beginning from not '0' + } + + // get rest + Bint b; + for ( ; i != 0; --i) { + b *= base; + b += t; + + a *= base; + t = a / denominator; + a %= denominator; + } + + return s + b.base(base); +} +/* + +std::string Bint::bin() const { + return "0b" + base2(1); +} + +std::string Bint::base2(const uint64_t base) const { + uint64_t mask = (1 << base) - 1; + std::string s = ""; + + Bint a(*this); + uint64_t j = a.number.size() << 6; + const uint64_t limit = j / base; + const uint64_t rmd = j % base; + j = 0; + + for(uint64_t i = 0; i < limit; i++) { + j = a.number[0] & mask; + s.push_back(alphabet[j]); + a.urshift(base); + } + + if (rmd) { + j = a.number[0] & mask; + s.push_back(alphabet[j]); + } + + if (s.size() == 0) return "0"; + std::reverse(s.begin(), s.end()); + return s; +} + + +std::string Bint::hex() const { + return "0x" + base2(4); +} + +std::string Bint::oct() const { + return "0" + base2(3); +} + +//*/ + +//////////////////////////////////////////////////////////////////////// +// INPUT-OUTPUT FUNCTIONS +//////////////////////////////////////////////////////////////////////// + +std::ostream& operator << (std::ostream& strm, const Brat& a) { + return strm << a.toString(); +} +/* +std::istream& operator >> (std::istream& strm, Brat& a) { + std::string s; + strm >> s; + a = s; + return strm; +} +//*/ + + +} // namespace diff --git a/tests/brat.cpp b/tests/brat.cpp new file mode 100644 index 0000000..4e1b746 --- /dev/null +++ b/tests/brat.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace TigerTV; + + +int main() { + Brat a("15", "98724"); + Brat b("34", "33339"); + Brat c = a + b; + std::cout << "a = " << a << std::endl; + std::cout << "b = " << b << std::endl; + std::cout << "c = " << c << std::endl; + std::cout << "a = " << a.point(10) << std::endl; + std::cout << "b = " << b.point(10) << std::endl; + std::cout << "c = " << c.point(50) << std::endl; + std::cout << std::endl; +/* + Bint a; + Bint b; + + uint64_t d = 4000; + std::cout << "a = " << d << std::endl; + std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); + a = Math::fact(d); + std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + + //assert(a.toString()=="6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000"); // 120! + //assert(a.toString()==s); + + //std::cout << "a = " << a << std::endl; + std::cout << "a b1000 = " << a.base(1000) << std::endl; + //std::cout << "s = " << "6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000" << std::endl; + / * + std::cout << "a bin = " << a.bin() << std::endl; + std::cout << "a hex = " << a.hex() << std::endl; + std::cout << "a oct = " << a.oct() << std::endl; + std::cout << "a b12 = " << a.base(12) << std::endl; + std::cout << "a b16 = " << a.base(16) << std::endl; + std::cout << "a b62 = " << a.base(62) << std::endl; + std::cout << "a b100 = " << a.base(100) << std::endl; + std::cout << "a b101 = " << a.base(101) << std::endl; + std::cout << "a b1001 = " << a.base(1001) << std::endl; + + std::cout << "Time difference = " << std::chrono::duration_cast(end - begin).count() << "[µs]" << std::endl; + std::cout << "Time difference = " << std::chrono::duration_cast (end - begin).count() << "[ns]" << std::endl; + std::cout << std::endl; + +//*/ + + + +} From 2a5c94ff5b43606876f650108f1c0c884a8ed9ea Mon Sep 17 00:00:00 2001 From: tigertv Date: Fri, 11 Dec 2020 20:39:54 +0200 Subject: [PATCH 03/19] Add a test for addition of Brat numbers --- src/Brat.cpp | 5 ++++ tests/brat-addition.cpp | 57 +++++++++++++++++++++++++++++++++++++++++ tests/brat.cpp | 7 +++++ 3 files changed, 69 insertions(+) create mode 100644 tests/brat-addition.cpp diff --git a/src/Brat.cpp b/src/Brat.cpp index 5800e78..ea277d1 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -39,6 +39,11 @@ void Brat::simplify() { numerator /= c; denominator /= c; } + + if (denominator < 0) { + denominator = -denominator; + numerator = -numerator; + } } /* diff --git a/tests/brat-addition.cpp b/tests/brat-addition.cpp new file mode 100644 index 0000000..427313f --- /dev/null +++ b/tests/brat-addition.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + + std::vector> data = { + {"1", "3", "1", "8", "11", "24"}, + {"2", "3", "3", "8", "25", "24"}, + {"2444234", "3434634563567456", "34563457727", "457536743223454565677473721", "559163491374441896220487035953613", "785735756188682526849306693345947725411888"}, + }; + + /////////////////////////////////////////////////////////// + + std::cout << "addition:" << std::endl; + + std::cout << "positives:" << std::endl; + for (auto& s : data) { + Brat a(s[0], s[1]); + std::cout << "a = " << a << std::endl; + Brat b(s[2], s[3]); + std::cout << "b = " << b << std::endl; + Brat t(s[4], s[5]); + std::cout << "t = " << t << std::endl; + + Brat c = a + b; + std::cout << "c = a + b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(c.toString() == t.toString()); + + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + Brat a("-" + s[0], s[1]); + std::cout << "a = " << a << std::endl; + Brat b(s[2], "-" + s[3]); + std::cout << "b = " << b << std::endl; + Brat t("-" + s[4], s[5]); + std::cout << "t = " << t << std::endl; + + Brat c = a + b; + std::cout << "c = a + b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(c.toString() == t.toString()); + + std::cout << std::endl; + } + +} diff --git a/tests/brat.cpp b/tests/brat.cpp index 4e1b746..e6170c7 100644 --- a/tests/brat.cpp +++ b/tests/brat.cpp @@ -21,6 +21,13 @@ int main() { std::cout << "b = " << b.point(10) << std::endl; std::cout << "c = " << c.point(50) << std::endl; std::cout << std::endl; + + Brat d("1", "3"); + std::cout << "d = " << d << std::endl; + d = 7 / d; + std::cout << "d = " << d << std::endl; + //d = d / 5; + //std::cout << "d = " << d << std::endl; /* Bint a; Bint b; From fa1644802bf0ac2fb7254eaf039c037323450502 Mon Sep 17 00:00:00 2001 From: tigertv Date: Sat, 12 Dec 2020 14:58:23 +0200 Subject: [PATCH 04/19] Add subtraction test for Brat --- src/Brat.cpp | 10 +++---- tests/brat-subtract.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ tests/gcd.cpp | 9 ++++-- 3 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 tests/brat-subtract.cpp diff --git a/src/Brat.cpp b/src/Brat.cpp index ea277d1..7f10d3d 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -34,16 +34,16 @@ Brat::Brat(const Bint& numerator, const Bint& denominator) : numerator(numerator } void Brat::simplify() { + if (denominator < 0) { + denominator = -denominator; + numerator = -numerator; + } + Bint c = Math::gcd(numerator, denominator); if (c != 1) { numerator /= c; denominator /= c; } - - if (denominator < 0) { - denominator = -denominator; - numerator = -numerator; - } } /* diff --git a/tests/brat-subtract.cpp b/tests/brat-subtract.cpp new file mode 100644 index 0000000..9bb5e4b --- /dev/null +++ b/tests/brat-subtract.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + + std::vector> data = { + {"1", "3", "1", "8", "5", "24"}, + {"2", "3", "3", "8", "7", "24"}, + {"3", "8", "2", "3", "-7", "24"}, + {"2", "3", "0", "8", "2", "3"}, + {"0", "3", "6", "16", "-3", "8"}, + {"2444234", "3434634563567456", "34563457727", "457536743223454565677473721", "559163372661595350663627267021101", "785735756188682526849306693345947725411888"}, + }; + + /////////////////////////////////////////////////////////// + + std::cout << "subtraction:" << std::endl; + + std::cout << "positives:" << std::endl; + for (auto& s : data) { + Brat a(s[0], s[1]); + std::cout << "a = " << a << std::endl; + Brat b(s[2], s[3]); + std::cout << "b = " << b << std::endl; + Brat t(s[4], s[5]); + std::cout << "t = " << t << std::endl; + + Brat c = a - b; + std::cout << "c = a - b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(c.toString() == t.toString()); + + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + Brat a(s[0], s[1]); + a = -a; + std::cout << "a = " << a << std::endl; + Brat b(s[2], s[3]); + b = -b; + std::cout << "b = " << b << std::endl; + + Brat t(s[4], s[5]); + t = -t; + std::cout << "t = " << t << std::endl; + + Brat c = a - b; + std::cout << "c = a - b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(c.toString() == t.toString()); + + std::cout << std::endl; + } + + +} diff --git a/tests/gcd.cpp b/tests/gcd.cpp index 11fa593..079d547 100644 --- a/tests/gcd.cpp +++ b/tests/gcd.cpp @@ -22,6 +22,9 @@ int main() { {"8345383455657547867846978956789567854653456345563456345634562456245634563456345634566163675675674674567456", "245345345345345345345345674678678567856785678567856730584300921369395234456456456454456", "8"}, {"6443534534546425624523462453567245728436724562456234563567456746734563456", "34245698729348249569258298624572785723745823845728452457824356", "4"}, {"4213518165211821182155201879854514971797088", "9307999046194775203602390818721305202949035", "123"}, + {"54", "-3", "3"}, + {"-54", "3", "3"}, + {"-44", "99", "11"}, }; /////////////////////////////////////////////////////////// @@ -46,7 +49,8 @@ int main() { std::cout << "negatives:" << std::endl; for (auto& s : data) { - a = "-"+s[0]; + a = s[0]; + a = -a; std::cout << "a = " << a << std::endl; std::cout << "a = " << a.bin() << std::endl; b = s[1]; @@ -64,7 +68,8 @@ int main() { a = s[0]; std::cout << "a = " << a << std::endl; std::cout << "a = " << a.bin() << std::endl; - b = "-"+s[1]; + b = s[1]; + b = -b; std::cout << "b = " << b << std::endl; std::cout << "b = " << b.bin() << std::endl; c = Math::gcd(a, b); From 40a38b595783e12660813a4be8fed5a868d1e004 Mon Sep 17 00:00:00 2001 From: tigertv Date: Sat, 12 Dec 2020 17:29:16 +0200 Subject: [PATCH 05/19] Add product test for Brat --- include/Bint/Brat.h | 4 +- src/Brat.cpp | 28 +++++++------- tests/brat-product.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 tests/brat-product.cpp diff --git a/include/Bint/Brat.h b/include/Bint/Brat.h index a73d098..74ae02d 100644 --- a/include/Bint/Brat.h +++ b/include/Bint/Brat.h @@ -41,7 +41,9 @@ class Brat { public: // Constructors - //Brat(); + Brat(); + Brat(const char* s); + Brat(const std::string& s); Brat(const Bint& numerator, const Bint& denominator); // String representation diff --git a/src/Brat.cpp b/src/Brat.cpp index 7f10d3d..68382f8 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -29,10 +29,25 @@ namespace TigerTV { +Brat::Brat() { + //numerator = 0; + denominator = 1; +} + Brat::Brat(const Bint& numerator, const Bint& denominator) : numerator(numerator), denominator(denominator) { simplify(); } +Brat::Brat(const std::string& s) { + auto end = s.find("/"); + numerator = s.substr(0, end); + denominator = s.substr(end + 1); +} + +Brat::Brat(const char* s) : Brat((std::string)s) { + +} + void Brat::simplify() { if (denominator < 0) { denominator = -denominator; @@ -46,18 +61,5 @@ void Brat::simplify() { } } -/* -Bint::Bint() { - number.push_back(0); -} - -Bint::Bint(const std::string& s) : Bint() { - -} - -Bint::Bint(const char* decimal) : Bint((std::string)decimal) { - -} -//*/ } // namespace diff --git a/tests/brat-product.cpp b/tests/brat-product.cpp new file mode 100644 index 0000000..4bae5e7 --- /dev/null +++ b/tests/brat-product.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + Brat a; + Brat b; + Brat c; + + std::vector> data = { + {"-500000000000000000000/200000000000000000000000000", "-50000000004000000000000/400000000000000000000" , "12500000001/40000000000000", "1/160000000000"}, + {"543563/24994884934885", "-537483929/484588458292993" , "-292156376899027/12112232755806678879056760805", "295460734969/624744272908141130689963225"}, + }; + + /////////////////////////////////////////////////////////// + std::cout << "product:" << std::endl; + + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + + Brat t = s[2]; + std::cout << "t = " << t << std::endl; + c = a * b; + std::cout << "c = " << c << std::endl; + assert(t.toString() == c.toString()); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + b = -b; + std::cout << "b = " << b << std::endl; + + Brat t = s[2]; + t = -t; + std::cout << "t = " << t << std::endl; + c = a * b; + std::cout << "c = " << c << std::endl; + assert(t.toString() == c.toString()); + std::cout << std::endl; + } + + /////////////////////////////////////////////////////////// + std::cout << "square:" << std::endl; + + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + + Brat t = s[3]; + std::cout << "t = " << t << std::endl; + c = a * a; + std::cout << "c = " << c << std::endl; + assert(t.toString() == c.toString()); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + a = -a; + std::cout << "a = " << a << std::endl; + + Brat t = s[3]; + std::cout << "t = " << t << std::endl; + c = a * a; + std::cout << "c = " << c << std::endl; + assert(t.toString() == c.toString()); + std::cout << std::endl; + } +} From 5a68938bb8da826ed802bba500787ce985d5a39e Mon Sep 17 00:00:00 2001 From: tigertv Date: Sat, 12 Dec 2020 17:49:51 +0200 Subject: [PATCH 06/19] Add division test for Brat --- tests/brat-division.cpp | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/brat-division.cpp diff --git a/tests/brat-division.cpp b/tests/brat-division.cpp new file mode 100644 index 0000000..17b0c8c --- /dev/null +++ b/tests/brat-division.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + Brat a; + Brat b; + Brat c; + + std::vector> data = { + {"-500000000000000000000/200000000000000000000000000", "-50000000004000000000000/400000000000000000000", "250/12500000001"}, + {"543563/24994884934885", "-537483929/484588458292993", "-263404356155114154059/13434348959704898963165"}, + }; + + /////////////////////////////////////////////////////////// + std::cout << "product:" << std::endl; + + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + + Brat t = s[2]; + std::cout << "t = " << t << std::endl; + c = a / b; + std::cout << "c = " << c << std::endl; + assert(t.toString() == c.toString()); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + b = -b; + std::cout << "b = " << b << std::endl; + + Brat t = s[2]; + t = -t; + std::cout << "t = " << t << std::endl; + c = a / b; + std::cout << "c = " << c << std::endl; + assert(t.toString() == c.toString()); + std::cout << std::endl; + } + +} From 0252e0a334c91715236c6c7c8a9209eeac78fa78 Mon Sep 17 00:00:00 2001 From: tigertv Date: Sat, 12 Dec 2020 18:41:43 +0200 Subject: [PATCH 07/19] Add comparison test for Brat --- include/Bint/Brat.h | 1 + src/Brat.cpp | 13 ++- tests/brat-comparison.cpp | 189 ++++++++++++++++++++++++++++++++++++++ tests/brat-division.cpp | 2 +- 4 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 tests/brat-comparison.cpp diff --git a/include/Bint/Brat.h b/include/Bint/Brat.h index 74ae02d..6110c10 100644 --- a/include/Bint/Brat.h +++ b/include/Bint/Brat.h @@ -42,6 +42,7 @@ class Brat { public: // Constructors Brat(); + Brat(const int64_t num); Brat(const char* s); Brat(const std::string& s); Brat(const Bint& numerator, const Bint& denominator); diff --git a/src/Brat.cpp b/src/Brat.cpp index 68382f8..ef66ce7 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -38,10 +38,19 @@ Brat::Brat(const Bint& numerator, const Bint& denominator) : numerator(numerator simplify(); } +Brat::Brat(const int64_t num) { + numerator = num; + denominator = 1; +} + Brat::Brat(const std::string& s) { - auto end = s.find("/"); + auto end = s.find('/'); numerator = s.substr(0, end); - denominator = s.substr(end + 1); + if (end != std::string::npos) { + denominator = s.substr(end + 1); + } else { + denominator = 1; + } } Brat::Brat(const char* s) : Brat((std::string)s) { diff --git a/tests/brat-comparison.cpp b/tests/brat-comparison.cpp new file mode 100644 index 0000000..e5df729 --- /dev/null +++ b/tests/brat-comparison.cpp @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + Brat a; + Brat b; + + std::vector> data = { + {"500000000000000000000/200000000000000000000000000", "50000000004000000000000/400000000000000000000", "0"}, + {"537483929/484588458292993", "543563/24994884934885", "1"}, + {"2/3", "6/11", "1"}, + {"2/3", "2/3", "0"}, + {"-2/3", "6/-11", "0"}, + }; + + /////////////////////////////////////////////////////////// + std::cout << "comparison:" << std::endl; + + std::cout << "operator >:" << std::endl; + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + + bool t = (s[2] != "0"); + std::cout << "t = " << t << std::endl; + bool c = a > b; + std::cout << "c = a > b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + a = -a; + std::cout << "a = " << a << std::endl; + b = s[1]; + b = -b; + std::cout << "b = " << b << std::endl; + + bool t = !(s[2] != "0"); + if (a == b) { + t = !t; + } + std::cout << "t = " << t << std::endl; + bool c = a > b; + std::cout << "c = a > b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } + + /////////////////////////////////////////////////////////// + + std::cout << "operator <=:" << std::endl; + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + + bool t = !(s[2] != "0"); + std::cout << "t = " << t << std::endl; + bool c = (a <= b); + std::cout << "c = a <= b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + a = -a; + std::cout << "a = " << a << std::endl; + b = s[1]; + b = -b; + std::cout << "b = " << b << std::endl; + + bool t = (s[2] != "0"); + if (a == b) { + t = !t; + } + std::cout << "t = " << t << std::endl; + bool c = (a <= b); + std::cout << "c = a <= b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } + + /////////////////////////////////////////////////////////// + + std::cout << "operator <:" << std::endl; + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + + bool t = !(s[2] != "0"); + if (a == b) { + t = false; + } + std::cout << "t = " << t << std::endl; + bool c = (a < b); + std::cout << "c = a < b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + a = -a; + std::cout << "a = " << a << std::endl; + b = s[1]; + b = -b; + std::cout << "b = " << b << std::endl; + + bool t = (s[2] != "0"); + if (a == b) { + t = false; + } + std::cout << "t = " << t << std::endl; + bool c = (a < b); + std::cout << "c = a < b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } + + /////////////////////////////////////////////////////////// + + std::cout << "operator >=:" << std::endl; + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + + bool t = (s[2] != "0"); + if (a == b) { + t = true; + } + std::cout << "t = " << t << std::endl; + bool c = (a >= b); + std::cout << "c = a >= b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + a = -a; + std::cout << "a = " << a << std::endl; + b = s[1]; + b = -b; + std::cout << "b = " << b << std::endl; + + bool t = !(s[2] != "0"); + if (a == b) { + t = true; + } + std::cout << "t = " << t << std::endl; + bool c = (a >= b); + std::cout << "c = a >= b" << std::endl; + std::cout << "c = " << c << std::endl; + assert(t == c); + std::cout << std::endl; + } +} diff --git a/tests/brat-division.cpp b/tests/brat-division.cpp index 17b0c8c..d16a7a9 100644 --- a/tests/brat-division.cpp +++ b/tests/brat-division.cpp @@ -19,7 +19,7 @@ int main() { }; /////////////////////////////////////////////////////////// - std::cout << "product:" << std::endl; + std::cout << "division:" << std::endl; std::cout << "positives:" << std::endl; for (auto& s : data) { From 048f3d8a6d713a39b2490e202d43c402db9e2f9e Mon Sep 17 00:00:00 2001 From: tigertv Date: Sat, 12 Dec 2020 21:49:13 +0200 Subject: [PATCH 08/19] Add gcd and lcm tests for Brat --- include/Bint/Brat.h | 4 ++++ include/Bint/Math.h | 3 +++ src/Brat.cpp | 8 +++++++ src/Math.cpp | 13 +++++++++++ tests/brat-gcd.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ tests/brat-lcm.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 tests/brat-gcd.cpp create mode 100644 tests/brat-lcm.cpp diff --git a/include/Bint/Brat.h b/include/Bint/Brat.h index 6110c10..682092e 100644 --- a/include/Bint/Brat.h +++ b/include/Bint/Brat.h @@ -47,6 +47,10 @@ class Brat { Brat(const std::string& s); Brat(const Bint& numerator, const Bint& denominator); + // getters + Bint getNumerator() const; + Bint getDenominator() const; + // String representation std::string toString() const; std::string base(const uint64_t base) const; diff --git a/include/Bint/Math.h b/include/Bint/Math.h index 96c66f9..7d171ee 100644 --- a/include/Bint/Math.h +++ b/include/Bint/Math.h @@ -28,6 +28,7 @@ #define TIGERTV_MATH_H #include "Bint.h" +#include "Brat.h" namespace TigerTV { @@ -41,7 +42,9 @@ class Math { static Bint modPow(const Bint& base, const uint64_t exp, const uint64_t mod); static Bint fact(const uint64_t a); static Bint gcd(const Bint& a, const Bint& b); + static Brat gcd(const Brat& a, const Brat& b); static Bint lcm(const Bint& a, const Bint& b); + static Brat lcm(const Brat& a, const Brat& b); static Bint fib(const uint64_t a); }; diff --git a/src/Brat.cpp b/src/Brat.cpp index ef66ce7..eccedd9 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -70,5 +70,13 @@ void Brat::simplify() { } } +Bint Brat::getNumerator() const { + return numerator; +} + +Bint Brat::getDenominator() const { + return denominator; +} + } // namespace diff --git a/src/Math.cpp b/src/Math.cpp index cf9b235..417a609 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -189,5 +189,18 @@ Bint Math::fib(const uint64_t a) { return fn; } +Brat Math::gcd(const Brat& a, const Brat& b) { + Bint numerator = gcd(a.getNumerator(), b.getNumerator()); + Bint denominator = lcm(a.getDenominator(), b.getDenominator()); + Brat c(numerator, denominator); + return c; +} + +Brat Math::lcm(const Brat& a, const Brat& b) { + Brat c = a * b / gcd(a, b); + Brat zero; + if (c < zero) return -c; + return c; +} } // namespace diff --git a/tests/brat-gcd.cpp b/tests/brat-gcd.cpp new file mode 100644 index 0000000..db55934 --- /dev/null +++ b/tests/brat-gcd.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + Brat a; + Brat b; + Brat c; + + std::vector> data = { + {"4/5", "8/15", "4/15"}, + {"4434/2345", "3422448/14345345", "6/961138115"}, + {"44123456789034/23123456789045", "34123456789022448/141234567890345345", "6/653166285546368606189972549105"}, + {"441234567890341234567/231234567890451234567", "341234567890224481234567/1412345678903453451234567", "1/108861047591062015671942869376375371818559163"}, + {"4412345678903412345672/2312345678904512345672", "3412345678902244812345672/14123456789034534512345672", "3/510286160583103198462673630487219350369017681"}, + {"4412345678903412345672345/2312345678904512345672", "3412345678902244812345672345/14123456789034534512345672", "15/4082289284664825587701389043897754802952141448"}, + }; + + /////////////////////////////////////////////////////////// + + std::cout << "Greatest Common Divisor:" << std::endl; + + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + c = Math::gcd(a, b); + std::cout << "c = " << c << std::endl; + std::cout << "s = " << s[2] << std::endl; + assert(c.toString()==s[2]); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + a = -a; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + c = Math::gcd(a, b); + std::cout << "c = " << c << std::endl; + std::cout << "s = " << s[2] << std::endl; + assert(c.toString()==s[2]); + std::cout << std::endl; + } + + +} diff --git a/tests/brat-lcm.cpp b/tests/brat-lcm.cpp new file mode 100644 index 0000000..74a2ca9 --- /dev/null +++ b/tests/brat-lcm.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + Brat a; + Brat b; + Brat c; + + std::vector> data = { + {"4/5", "8/15", "8/5"}, + {"4434/2345", "3422448/14345345", "2529189072/35"}, + {"44123456789034/23123456789045", "34123456789022448/141234567890345345", "250940811853816811901637705872/5"}, + {"441234567890341234567/231234567890451234567", "341234567890224481234567/1412345678903453451234567", "150564487112290508962653036830640507095677489/3"}, + {"4412345678903412345672/2312345678904512345672", "3412345678902244812345672/14123456789034534512345672", "78419003704317973418084047912047984406339227/1"}, + {"4412345678903412345672345/2312345678904512345672", "3412345678902244812345672345/14123456789034534512345672", "1003763247415270059751554398708779566810310006519935/8"}, + }; + + /////////////////////////////////////////////////////////// + + std::cout << "Least Common Multiple:" << std::endl; + + std::cout << "positives:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + c = Math::lcm(a, b); + std::cout << "c = " << c << std::endl; + std::cout << "s = " << s[2] << std::endl; + assert(c.toString() == s[2]); + std::cout << std::endl; + } + + std::cout << "negatives:" << std::endl; + for (auto& s : data) { + a = s[0]; + a = -a; + std::cout << "a = " << a << std::endl; + b = s[1]; + std::cout << "b = " << b << std::endl; + c = Math::lcm(a, b); + std::cout << "c = " << c << std::endl; + std::cout << "s = " << s[2] << std::endl; + assert(c.toString()==s[2]); + std::cout << std::endl; + } + + +} From 8ad43073d2fd8ab35d7462f8a7656a69c050d9ab Mon Sep 17 00:00:00 2001 From: tigertv Date: Sun, 13 Dec 2020 00:37:23 +0200 Subject: [PATCH 09/19] Add pow test for Brat --- include/Bint/Math.h | 3 ++- src/Math.cpp | 7 +++++++ tests/brat-pows.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/brat-pows.cpp diff --git a/include/Bint/Math.h b/include/Bint/Math.h index 7d171ee..f8b6204 100644 --- a/include/Bint/Math.h +++ b/include/Bint/Math.h @@ -38,7 +38,8 @@ class Math { Math(); static Bint oddFact(const uint64_t a, const uint64_t begin); public: - static Bint pow(const Bint& a, uint64_t pow); + static Bint pow(const Bint& a, uint64_t exp); + static Brat pow(const Brat& a, uint64_t exp); static Bint modPow(const Bint& base, const uint64_t exp, const uint64_t mod); static Bint fact(const uint64_t a); static Bint gcd(const Bint& a, const Bint& b); diff --git a/src/Math.cpp b/src/Math.cpp index 417a609..4ed65b2 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -203,4 +203,11 @@ Brat Math::lcm(const Brat& a, const Brat& b) { return c; } +Brat Math::pow(const Brat& a, uint64_t exp) { + Bint b = Math::pow(a.getNumerator(), exp); + Bint c = Math::pow(a.getDenominator(), exp); + Brat d(b, c); + return d; +} + } // namespace diff --git a/tests/brat-pows.cpp b/tests/brat-pows.cpp new file mode 100644 index 0000000..ad38346 --- /dev/null +++ b/tests/brat-pows.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace TigerTV; + + +int main() { + Brat a; + int b; + Brat c; + + std::vector> data = { + {"-5/23", "334" , "285746847820568745553945887076335239862977498705699665303781460806908158976684548918862005465043007412295763588343507058055135340570979687781510573352510310491378504344021315252113959925738006541406921456882628262974321842193603515625/65629218059151821783361066374230705132484876048854031364279109080760000432549452817323157819336406081525296568668713099971626516034722654896387618155178603081327877047438815730857894400750808295634321357126661155038480159766714286970178265202351119940657886462335013967533238982725161694721534974960914781713905854639939552518927584726107453798646114783787779979965748010815548352462018323575289721202724893333330184001516901514121741731791414098158218609"}, + }; + + /////////////////////////////////////////////////////////// + std::cout << "pows:" << std::endl; + + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + b = std::stoi(s[1]); + std::cout << "b = " << b << std::endl; + + Brat t = s[2]; + std::cout << "t = " << t << std::endl; + c = Math::pow(a, b); + std::cout << "c = " << c << std::endl; + assert(t.toString() == c.toString()); + std::cout << std::endl; + } + +} From 6c4a142fef3ef59ef9370a68db5cdea7f6547ef1 Mon Sep 17 00:00:00 2001 From: tigertv Date: Sun, 13 Dec 2020 01:39:07 +0200 Subject: [PATCH 10/19] Rename repository 'Bint' replaced by 'BeeNum' --- README.md | 10 +++++----- include/{Bint => BeeNum}/Bint.h | 0 include/{Bint => BeeNum}/Brat.h | 0 include/{Bint => BeeNum}/Math.h | 0 src/Bint.cpp | 2 +- src/Bint/arithmetics.cpp | 2 +- src/Bint/bitoperations.cpp | 2 +- src/Bint/comparison.cpp | 2 +- src/Bint/io.cpp | 2 +- src/Bint/shifts.cpp | 2 +- src/Brat.cpp | 4 ++-- src/Brat/arithmetics.cpp | 2 +- src/Brat/comparison.cpp | 2 +- src/Brat/io.cpp | 3 +-- src/Math.cpp | 2 +- tests/addition.cpp | 2 +- tests/base.cpp | 4 ++-- tests/bits.cpp | 2 +- tests/brat-addition.cpp | 2 +- tests/brat-comparison.cpp | 2 +- tests/brat-division.cpp | 2 +- tests/brat-gcd.cpp | 4 ++-- tests/brat-lcm.cpp | 4 ++-- tests/brat-pows.cpp | 5 ++--- tests/brat-product.cpp | 3 +-- tests/brat-subtract.cpp | 2 +- tests/brat.cpp | 4 ++-- tests/comparison.cpp | 3 +-- tests/division.cpp | 3 +-- tests/factorial.cpp | 4 ++-- tests/fibonnaci.cpp | 4 ++-- tests/gcd.cpp | 4 ++-- tests/lcm.cpp | 4 ++-- tests/main.cpp | 4 ++-- tests/pows.cpp | 4 ++-- tests/product.cpp | 3 +-- tests/shifts.cpp | 3 +-- tests/subtract.cpp | 6 ++---- 38 files changed, 52 insertions(+), 61 deletions(-) rename include/{Bint => BeeNum}/Bint.h (100%) rename include/{Bint => BeeNum}/Brat.h (100%) rename include/{Bint => BeeNum}/Math.h (100%) diff --git a/README.md b/README.md index 5037b6d..2f439d1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Bint +# BeeNum [![travis][travis-shield]][travis-link] [![github-actions][github-shield]][github-link] @@ -51,7 +51,7 @@ Time difference = 90651[µs] (g++ -O0) Time difference = 15621[µs] (g++ -Ofast) ``` -[travis-shield]: https://img.shields.io/travis/tigertv/Bint/master.svg?style=for-the-badge&logo=travis -[travis-link]: https://travis-ci.org/tigertv/Bint -[github-shield]: https://img.shields.io/github/workflow/status/tigertv/Bint/Release.svg?style=for-the-badge&logo=github -[github-link]: https://github.com/tigertv/Bint/actions +[travis-shield]: https://img.shields.io/travis/tigertv/BeeNum/master.svg?style=for-the-badge&logo=travis +[travis-link]: https://travis-ci.org/tigertv/BeeNum +[github-shield]: https://img.shields.io/github/workflow/status/tigertv/BeeNum/Release.svg?style=for-the-badge&logo=github +[github-link]: https://github.com/tigertv/BeeNum/actions diff --git a/include/Bint/Bint.h b/include/BeeNum/Bint.h similarity index 100% rename from include/Bint/Bint.h rename to include/BeeNum/Bint.h diff --git a/include/Bint/Brat.h b/include/BeeNum/Brat.h similarity index 100% rename from include/Bint/Brat.h rename to include/BeeNum/Brat.h diff --git a/include/Bint/Math.h b/include/BeeNum/Math.h similarity index 100% rename from include/Bint/Math.h rename to include/BeeNum/Math.h diff --git a/src/Bint.cpp b/src/Bint.cpp index c8b1c22..05d1d1d 100644 --- a/src/Bint.cpp +++ b/src/Bint.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include #include diff --git a/src/Bint/arithmetics.cpp b/src/Bint/arithmetics.cpp index cad5c9b..6e80f15 100644 --- a/src/Bint/arithmetics.cpp +++ b/src/Bint/arithmetics.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include namespace TigerTV { diff --git a/src/Bint/bitoperations.cpp b/src/Bint/bitoperations.cpp index 003c260..ba38db4 100644 --- a/src/Bint/bitoperations.cpp +++ b/src/Bint/bitoperations.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include namespace TigerTV { diff --git a/src/Bint/comparison.cpp b/src/Bint/comparison.cpp index ea87a8a..8085805 100644 --- a/src/Bint/comparison.cpp +++ b/src/Bint/comparison.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include namespace TigerTV { diff --git a/src/Bint/io.cpp b/src/Bint/io.cpp index 895f8e4..8d88710 100644 --- a/src/Bint/io.cpp +++ b/src/Bint/io.cpp @@ -24,7 +24,7 @@ */ #include -#include +#include namespace TigerTV { diff --git a/src/Bint/shifts.cpp b/src/Bint/shifts.cpp index ed0e550..1717eb0 100644 --- a/src/Bint/shifts.cpp +++ b/src/Bint/shifts.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include namespace TigerTV { diff --git a/src/Brat.cpp b/src/Brat.cpp index eccedd9..1aa016f 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -23,8 +23,8 @@ * SOFTWARE. */ -#include -#include +#include +#include namespace TigerTV { diff --git a/src/Brat/arithmetics.cpp b/src/Brat/arithmetics.cpp index eb276ad..28937fc 100644 --- a/src/Brat/arithmetics.cpp +++ b/src/Brat/arithmetics.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include namespace TigerTV { diff --git a/src/Brat/comparison.cpp b/src/Brat/comparison.cpp index 0fa4dec..a78d6ce 100644 --- a/src/Brat/comparison.cpp +++ b/src/Brat/comparison.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include namespace TigerTV { diff --git a/src/Brat/io.cpp b/src/Brat/io.cpp index 399f21f..4e2b629 100644 --- a/src/Brat/io.cpp +++ b/src/Brat/io.cpp @@ -23,8 +23,7 @@ * SOFTWARE. */ -//#include -#include +#include namespace TigerTV { diff --git a/src/Math.cpp b/src/Math.cpp index 4ed65b2..3462fad 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -23,7 +23,7 @@ * SOFTWARE. */ -#include +#include namespace TigerTV { diff --git a/tests/addition.cpp b/tests/addition.cpp index 84de287..a3a6a1a 100644 --- a/tests/addition.cpp +++ b/tests/addition.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include using namespace TigerTV; diff --git a/tests/base.cpp b/tests/base.cpp index 5283b8c..9e09656 100644 --- a/tests/base.cpp +++ b/tests/base.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/bits.cpp b/tests/bits.cpp index f89259c..d0489d3 100644 --- a/tests/bits.cpp +++ b/tests/bits.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include using namespace TigerTV; diff --git a/tests/brat-addition.cpp b/tests/brat-addition.cpp index 427313f..e4559e9 100644 --- a/tests/brat-addition.cpp +++ b/tests/brat-addition.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include using namespace TigerTV; diff --git a/tests/brat-comparison.cpp b/tests/brat-comparison.cpp index e5df729..cc7df1a 100644 --- a/tests/brat-comparison.cpp +++ b/tests/brat-comparison.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include using namespace TigerTV; diff --git a/tests/brat-division.cpp b/tests/brat-division.cpp index d16a7a9..0d0017a 100644 --- a/tests/brat-division.cpp +++ b/tests/brat-division.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include using namespace TigerTV; diff --git a/tests/brat-gcd.cpp b/tests/brat-gcd.cpp index db55934..b2b43d6 100644 --- a/tests/brat-gcd.cpp +++ b/tests/brat-gcd.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/brat-lcm.cpp b/tests/brat-lcm.cpp index 74a2ca9..6a3abde 100644 --- a/tests/brat-lcm.cpp +++ b/tests/brat-lcm.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/brat-pows.cpp b/tests/brat-pows.cpp index ad38346..8eb6113 100644 --- a/tests/brat-pows.cpp +++ b/tests/brat-pows.cpp @@ -2,9 +2,8 @@ #include #include #include -#include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/brat-product.cpp b/tests/brat-product.cpp index 4bae5e7..1a73b9b 100644 --- a/tests/brat-product.cpp +++ b/tests/brat-product.cpp @@ -2,8 +2,7 @@ #include #include #include -#include -#include +#include using namespace TigerTV; diff --git a/tests/brat-subtract.cpp b/tests/brat-subtract.cpp index 9bb5e4b..e187ef7 100644 --- a/tests/brat-subtract.cpp +++ b/tests/brat-subtract.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include using namespace TigerTV; diff --git a/tests/brat.cpp b/tests/brat.cpp index e6170c7..cc78298 100644 --- a/tests/brat.cpp +++ b/tests/brat.cpp @@ -3,8 +3,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/comparison.cpp b/tests/comparison.cpp index fbadc7a..4b9281c 100644 --- a/tests/comparison.cpp +++ b/tests/comparison.cpp @@ -2,8 +2,7 @@ #include #include #include -#include -#include +#include using namespace TigerTV; diff --git a/tests/division.cpp b/tests/division.cpp index a19f586..0f03337 100644 --- a/tests/division.cpp +++ b/tests/division.cpp @@ -2,8 +2,7 @@ #include #include #include -#include -#include +#include using namespace TigerTV; diff --git a/tests/factorial.cpp b/tests/factorial.cpp index 364883d..6dbe7f8 100644 --- a/tests/factorial.cpp +++ b/tests/factorial.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include #include using namespace TigerTV; diff --git a/tests/fibonnaci.cpp b/tests/fibonnaci.cpp index ab6cc0d..f585a2b 100644 --- a/tests/fibonnaci.cpp +++ b/tests/fibonnaci.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/gcd.cpp b/tests/gcd.cpp index 079d547..d8803e7 100644 --- a/tests/gcd.cpp +++ b/tests/gcd.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/lcm.cpp b/tests/lcm.cpp index 6ff55fa..2a15e2d 100644 --- a/tests/lcm.cpp +++ b/tests/lcm.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/main.cpp b/tests/main.cpp index 6e01bd7..c932496 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include #include using namespace TigerTV; diff --git a/tests/pows.cpp b/tests/pows.cpp index 334a66b..4d822c5 100644 --- a/tests/pows.cpp +++ b/tests/pows.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include using namespace TigerTV; diff --git a/tests/product.cpp b/tests/product.cpp index 1a15fa6..403672b 100644 --- a/tests/product.cpp +++ b/tests/product.cpp @@ -2,8 +2,7 @@ #include #include #include -#include -#include +#include using namespace TigerTV; diff --git a/tests/shifts.cpp b/tests/shifts.cpp index c6d1589..191c8d4 100644 --- a/tests/shifts.cpp +++ b/tests/shifts.cpp @@ -2,8 +2,7 @@ #include #include #include -#include -#include +#include using namespace TigerTV; diff --git a/tests/subtract.cpp b/tests/subtract.cpp index 22bcc4b..e988aa8 100644 --- a/tests/subtract.cpp +++ b/tests/subtract.cpp @@ -2,8 +2,7 @@ #include #include #include -#include "Bint/Bint.h" -#include +#include using namespace TigerTV; @@ -13,8 +12,7 @@ int main() { Bint b; Bint c; - std::map data; - data = { + std::map data = { {61, "2305843009213693952"}, {62, "4611686018427387904"}, {63, "9223372036854775808"}, {64, "18446744073709551616"}, {65, "36893488147419103232"} }; From c6738f1da8265b09269d846853240686f101a8c0 Mon Sep 17 00:00:00 2001 From: tigertv Date: Sun, 13 Dec 2020 18:45:07 +0200 Subject: [PATCH 11/19] Change namespace 'TigerTV' replaced by 'BeeNum' --- include/BeeNum/Bint.h | 4 ++-- include/BeeNum/Brat.h | 4 ++-- include/BeeNum/Math.h | 4 ++-- src/Bint.cpp | 4 ++-- src/Bint/arithmetics.cpp | 5 +++-- src/Bint/bitoperations.cpp | 4 ++-- src/Bint/comparison.cpp | 5 +++-- src/Bint/io.cpp | 5 +++-- src/Bint/shifts.cpp | 4 ++-- src/Brat.cpp | 4 ++-- src/Brat/arithmetics.cpp | 4 ++-- src/Brat/comparison.cpp | 4 ++-- src/Brat/io.cpp | 5 +++-- src/Math.cpp | 4 ++-- tests/addition.cpp | 2 +- tests/base.cpp | 2 +- tests/bits.cpp | 2 +- tests/brat-addition.cpp | 2 +- tests/brat-comparison.cpp | 2 +- tests/brat-division.cpp | 2 +- tests/brat-gcd.cpp | 2 +- tests/brat-lcm.cpp | 2 +- tests/brat-pows.cpp | 2 +- tests/brat-product.cpp | 2 +- tests/brat-subtract.cpp | 2 +- tests/brat.cpp | 2 +- tests/comparison.cpp | 2 +- tests/division.cpp | 2 +- tests/factorial.cpp | 2 +- tests/fibonnaci.cpp | 2 +- tests/gcd.cpp | 2 +- tests/lcm.cpp | 2 +- tests/main.cpp | 2 +- tests/pows.cpp | 2 +- tests/product.cpp | 2 +- tests/shifts.cpp | 2 +- tests/subtract.cpp | 2 +- 37 files changed, 55 insertions(+), 51 deletions(-) diff --git a/include/BeeNum/Bint.h b/include/BeeNum/Bint.h index ca5c018..6d33c53 100644 --- a/include/BeeNum/Bint.h +++ b/include/BeeNum/Bint.h @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -33,7 +33,7 @@ #include -namespace TigerTV { +namespace BeeNum { class Bint { diff --git a/include/BeeNum/Brat.h b/include/BeeNum/Brat.h index 682092e..3e7874b 100644 --- a/include/BeeNum/Brat.h +++ b/include/BeeNum/Brat.h @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -30,7 +30,7 @@ #include "Bint.h" -namespace TigerTV { +namespace BeeNum { class Brat { private: diff --git a/include/BeeNum/Math.h b/include/BeeNum/Math.h index f8b6204..b35a720 100644 --- a/include/BeeNum/Math.h +++ b/include/BeeNum/Math.h @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -31,7 +31,7 @@ #include "Brat.h" -namespace TigerTV { +namespace BeeNum { class Math { private: diff --git a/src/Bint.cpp b/src/Bint.cpp index 05d1d1d..116644a 100644 --- a/src/Bint.cpp +++ b/src/Bint.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -27,7 +27,7 @@ #include -namespace TigerTV { +namespace BeeNum { const std::string Bint::alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; diff --git a/src/Bint/arithmetics.cpp b/src/Bint/arithmetics.cpp index 6e80f15..5e39075 100644 --- a/src/Bint/arithmetics.cpp +++ b/src/Bint/arithmetics.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,7 +25,8 @@ #include -namespace TigerTV { + +namespace BeeNum { Bint Bint::operator + (const Bint& a) const { Bint b = *this; diff --git a/src/Bint/bitoperations.cpp b/src/Bint/bitoperations.cpp index ba38db4..9bd77c9 100644 --- a/src/Bint/bitoperations.cpp +++ b/src/Bint/bitoperations.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,8 +25,8 @@ #include -namespace TigerTV { +namespace BeeNum { Bint& Bint::bitOperation(const Bint& a, std::function&& lambda) { Bint aa(a); diff --git a/src/Bint/comparison.cpp b/src/Bint/comparison.cpp index 8085805..60666c5 100644 --- a/src/Bint/comparison.cpp +++ b/src/Bint/comparison.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,7 +25,8 @@ #include -namespace TigerTV { + +namespace BeeNum { bool Bint::operator == (const int64_t a) const { if (number.size() != 1) return false; diff --git a/src/Bint/io.cpp b/src/Bint/io.cpp index 8d88710..6e1937a 100644 --- a/src/Bint/io.cpp +++ b/src/Bint/io.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -26,7 +26,8 @@ #include #include -namespace TigerTV { + +namespace BeeNum { Bint::operator std::string() const { return this->toString(); diff --git a/src/Bint/shifts.cpp b/src/Bint/shifts.cpp index 1717eb0..11c1855 100644 --- a/src/Bint/shifts.cpp +++ b/src/Bint/shifts.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,8 +25,8 @@ #include -namespace TigerTV { +namespace BeeNum { Bint Bint::operator >> (const uint64_t shift) const { Bint b(*this); diff --git a/src/Brat.cpp b/src/Brat.cpp index 1aa016f..ffb2d92 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -27,7 +27,7 @@ #include -namespace TigerTV { +namespace BeeNum { Brat::Brat() { //numerator = 0; diff --git a/src/Brat/arithmetics.cpp b/src/Brat/arithmetics.cpp index 28937fc..d6e8b0c 100644 --- a/src/Brat/arithmetics.cpp +++ b/src/Brat/arithmetics.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -26,7 +26,7 @@ #include -namespace TigerTV { +namespace BeeNum { Brat Brat::operator + (const Brat& a) const { Brat b = *this; diff --git a/src/Brat/comparison.cpp b/src/Brat/comparison.cpp index a78d6ce..60c7e32 100644 --- a/src/Brat/comparison.cpp +++ b/src/Brat/comparison.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -26,7 +26,7 @@ #include -namespace TigerTV { +namespace BeeNum { bool Brat::operator == (const Brat& b) const { return (numerator == b.numerator && denominator == b.denominator); diff --git a/src/Brat/io.cpp b/src/Brat/io.cpp index 4e2b629..fbd94ca 100644 --- a/src/Brat/io.cpp +++ b/src/Brat/io.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,7 +25,8 @@ #include -namespace TigerTV { + +namespace BeeNum { Brat::operator std::string() const { return this->toString(); diff --git a/src/Math.cpp b/src/Math.cpp index 3462fad..2466ddd 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -1,7 +1,7 @@ /* * MIT License * - * This file is part of the big-integer (https://github.com/tigertv/big-integer). + * This file is part of the BeeNum (https://github.com/tigertv/BeeNum). * Copyright (c) 2020 Max Vetrov(tigertv). * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -26,7 +26,7 @@ #include -namespace TigerTV { +namespace BeeNum { Bint Math::pow(const Bint& a, uint64_t exp) { diff --git a/tests/addition.cpp b/tests/addition.cpp index a3a6a1a..d1c8083 100644 --- a/tests/addition.cpp +++ b/tests/addition.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/base.cpp b/tests/base.cpp index 9e09656..6f97e1b 100644 --- a/tests/base.cpp +++ b/tests/base.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/bits.cpp b/tests/bits.cpp index d0489d3..596f6be 100644 --- a/tests/bits.cpp +++ b/tests/bits.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-addition.cpp b/tests/brat-addition.cpp index e4559e9..50e823e 100644 --- a/tests/brat-addition.cpp +++ b/tests/brat-addition.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-comparison.cpp b/tests/brat-comparison.cpp index cc7df1a..d92af48 100644 --- a/tests/brat-comparison.cpp +++ b/tests/brat-comparison.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-division.cpp b/tests/brat-division.cpp index 0d0017a..3488134 100644 --- a/tests/brat-division.cpp +++ b/tests/brat-division.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-gcd.cpp b/tests/brat-gcd.cpp index b2b43d6..7a716fb 100644 --- a/tests/brat-gcd.cpp +++ b/tests/brat-gcd.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-lcm.cpp b/tests/brat-lcm.cpp index 6a3abde..d0aaa70 100644 --- a/tests/brat-lcm.cpp +++ b/tests/brat-lcm.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-pows.cpp b/tests/brat-pows.cpp index 8eb6113..d87a8a7 100644 --- a/tests/brat-pows.cpp +++ b/tests/brat-pows.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-product.cpp b/tests/brat-product.cpp index 1a73b9b..8179799 100644 --- a/tests/brat-product.cpp +++ b/tests/brat-product.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-subtract.cpp b/tests/brat-subtract.cpp index e187ef7..2492376 100644 --- a/tests/brat-subtract.cpp +++ b/tests/brat-subtract.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat.cpp b/tests/brat.cpp index cc78298..7753ea0 100644 --- a/tests/brat.cpp +++ b/tests/brat.cpp @@ -7,7 +7,7 @@ #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/comparison.cpp b/tests/comparison.cpp index 4b9281c..ff49c33 100644 --- a/tests/comparison.cpp +++ b/tests/comparison.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/division.cpp b/tests/division.cpp index 0f03337..1fbf744 100644 --- a/tests/division.cpp +++ b/tests/division.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/factorial.cpp b/tests/factorial.cpp index 6dbe7f8..229b9d6 100644 --- a/tests/factorial.cpp +++ b/tests/factorial.cpp @@ -6,7 +6,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/fibonnaci.cpp b/tests/fibonnaci.cpp index f585a2b..60aff2d 100644 --- a/tests/fibonnaci.cpp +++ b/tests/fibonnaci.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/gcd.cpp b/tests/gcd.cpp index d8803e7..07a6754 100644 --- a/tests/gcd.cpp +++ b/tests/gcd.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/lcm.cpp b/tests/lcm.cpp index 2a15e2d..a50f3bc 100644 --- a/tests/lcm.cpp +++ b/tests/lcm.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/main.cpp b/tests/main.cpp index c932496..4049e49 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -6,7 +6,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/pows.cpp b/tests/pows.cpp index 4d822c5..a5354b8 100644 --- a/tests/pows.cpp +++ b/tests/pows.cpp @@ -5,7 +5,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/product.cpp b/tests/product.cpp index 403672b..2deb87f 100644 --- a/tests/product.cpp +++ b/tests/product.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/shifts.cpp b/tests/shifts.cpp index 191c8d4..92c9b96 100644 --- a/tests/shifts.cpp +++ b/tests/shifts.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/subtract.cpp b/tests/subtract.cpp index e988aa8..c8afa28 100644 --- a/tests/subtract.cpp +++ b/tests/subtract.cpp @@ -4,7 +4,7 @@ #include #include -using namespace TigerTV; +using namespace BeeNum; int main() { From 70094fa2e8814938f9adf65a88a68a0a0b09c172 Mon Sep 17 00:00:00 2001 From: tigertv Date: Mon, 14 Dec 2020 20:36:47 +0200 Subject: [PATCH 12/19] Add base and point tests - Add Brat's base test - Add test for Brat's point - Output Brat point for any base --- include/BeeNum/Brat.h | 1 + src/Brat.cpp | 1 + src/Brat/io.cpp | 53 ++++++------------------------------------- tests/base.cpp | 1 - tests/brat-base.cpp | 41 +++++++++++++++++++++++++++++++++ tests/brat-point.cpp | 49 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 47 deletions(-) create mode 100644 tests/brat-base.cpp create mode 100644 tests/brat-point.cpp diff --git a/include/BeeNum/Brat.h b/include/BeeNum/Brat.h index 3e7874b..51536ef 100644 --- a/include/BeeNum/Brat.h +++ b/include/BeeNum/Brat.h @@ -55,6 +55,7 @@ class Brat { std::string toString() const; std::string base(const uint64_t base) const; std::string point(const uint64_t num) const; + std::string point(const uint64_t num, const uint64_t base) const; operator std::string() const; // Arithmetics diff --git a/src/Brat.cpp b/src/Brat.cpp index ffb2d92..61e0b1e 100644 --- a/src/Brat.cpp +++ b/src/Brat.cpp @@ -51,6 +51,7 @@ Brat::Brat(const std::string& s) { } else { denominator = 1; } + simplify(); } Brat::Brat(const char* s) : Brat((std::string)s) { diff --git a/src/Brat/io.cpp b/src/Brat/io.cpp index fbd94ca..847e566 100644 --- a/src/Brat/io.cpp +++ b/src/Brat/io.cpp @@ -41,12 +41,15 @@ std::string Brat::base(const uint64_t base) const { } std::string Brat::point(const uint64_t num) const { - int base = 10; + return point(num, 10); +} + +std::string Brat::point(const uint64_t num, const uint64_t base) const { Bint a = numerator; Bint t = a / denominator; a %= denominator; - std::string s = t.toString() + '.'; - std::string temp; + std::string s = t.base(base); + s = s.substr(0, s.find('_')) + '.'; uint64_t i; // get zeros @@ -55,7 +58,7 @@ std::string Brat::point(const uint64_t num) const { t = a / denominator; a %= denominator; if (t != 0) break; - s += '0'; // wrong for alphabet beginning from not '0' + s += '0'; // TODO: replace, it's wrong for alphabet beginning from not '0' } // get rest @@ -71,48 +74,6 @@ std::string Brat::point(const uint64_t num) const { return s + b.base(base); } -/* - -std::string Bint::bin() const { - return "0b" + base2(1); -} - -std::string Bint::base2(const uint64_t base) const { - uint64_t mask = (1 << base) - 1; - std::string s = ""; - - Bint a(*this); - uint64_t j = a.number.size() << 6; - const uint64_t limit = j / base; - const uint64_t rmd = j % base; - j = 0; - - for(uint64_t i = 0; i < limit; i++) { - j = a.number[0] & mask; - s.push_back(alphabet[j]); - a.urshift(base); - } - - if (rmd) { - j = a.number[0] & mask; - s.push_back(alphabet[j]); - } - - if (s.size() == 0) return "0"; - std::reverse(s.begin(), s.end()); - return s; -} - - -std::string Bint::hex() const { - return "0x" + base2(4); -} - -std::string Bint::oct() const { - return "0" + base2(3); -} - -//*/ //////////////////////////////////////////////////////////////////////// // INPUT-OUTPUT FUNCTIONS diff --git a/tests/base.cpp b/tests/base.cpp index 6f97e1b..b146447 100644 --- a/tests/base.cpp +++ b/tests/base.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace BeeNum; diff --git a/tests/brat-base.cpp b/tests/brat-base.cpp new file mode 100644 index 0000000..5ee7dbd --- /dev/null +++ b/tests/brat-base.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +int main() { + Brat a; + Brat c; + + std::vector> data = { + {"453453a_b32/43453b_b32", "4466021482/137499755", "453453a_b32/43453b_b32"}, + {"-453453a_b32/-45afA3a_b62", "171770057/8920503206", "53q069_b32/89r84t6_b32"}, + {"1:12:20:111_b150/1:12:20:112_b150", "3648111/3648112", "3fajf_b32/3fajg_b32"}, + }; + + /////////////////////////////////////////////////////////// + + std::cout << "Different Bases:" << std::endl; + std::cout << "Decimal:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + std::cout << "s = " << s[1] << std::endl; + assert(a.toString() == s[1]); + std::cout << std::endl; + } + + std::cout << "Base32:" << std::endl; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a.base(32) << std::endl; + std::cout << "s = " << s[2] << std::endl; + assert(a.base(32) == s[2]); + std::cout << std::endl; + } + +} diff --git a/tests/brat-point.cpp b/tests/brat-point.cpp new file mode 100644 index 0000000..d8971ca --- /dev/null +++ b/tests/brat-point.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +int main() { + Brat a; + Brat c; + + std::vector> data = { + {"453453/434538", "1.04352898940944175192963561299587147729312511218811", "1.1kevzj90iucwsuh3jpribijw1mmg61qt71c5kztpvp2duatfet_b36"}, + {"171770057/8920503206", "0.01925564657433967632610253892890086810647574134171", "0.0oye3bbxc9ql9aq5wf6pcs85eirfkx3mojty0nze25udix3o6q_b36"}, + {"3648111/3648112", "0.99999972588560877516918340226396558000412268044402", "0.zzzzjfb96azavcnmk4nog2c7q7zyq5wghfp6i9805vzpdrcw1e_b36"}, + {"364811100000/3648112", "99999.97258856087751691834022639655800041226804440214554", "255r.z0h3b3ihxp0vodkay768y1nrfhr9lcttlmjblmude4q62lh9mu_b36"}, + }; + + /////////////////////////////////////////////////////////// + + std::cout << "Point:" << std::endl; + std::cout << "Decimal:" << std::endl; + int p = 50; + + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + std::cout << "a.point = " << a.point(p) << std::endl; + std::cout << "s = " << s[1] << std::endl; + assert(a.point(p) == s[1]); + std::cout << std::endl; + } + + std::cout << "Base36:" << std::endl; + int b = 36; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + std::cout << "a.point = " << a.point(p) << std::endl; + std::cout << "a.point = " << a.point(p, b) << std::endl; + std::cout << "s = " << s[1] << std::endl; + std::cout << "s = " << s[2] << std::endl; + assert(a.point(p, b) == s[2]); + std::cout << std::endl; + } + +} From 3bf2f74cc9cddfd5822da3501ad2e322bf2173eb Mon Sep 17 00:00:00 2001 From: tigertv Date: Tue, 15 Dec 2020 11:44:36 +0200 Subject: [PATCH 13/19] Fix Brats' point() - Fix sign and if base is more than size of alphabet, then number's wrong output occurs - Add rational numbers with point() usage in Readme --- README.md | 27 +++++++++++++++++++- src/Brat/io.cpp | 11 +++++++-- tests/brat-point.cpp | 23 ++++++++++++++--- tests/brat.cpp | 59 ++++++-------------------------------------- 4 files changed, 62 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 2f439d1..2c8174a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ [![travis][travis-shield]][travis-link] [![github-actions][github-shield]][github-link] -An implementation of BigInteger library in C++ +BeeNum is an arbitrary-precision arithmetic library. + +Integers: ```cpp Bint a = "372542872459"; @@ -51,6 +53,29 @@ Time difference = 90651[µs] (g++ -O0) Time difference = 15621[µs] (g++ -Ofast) ``` +Rationals(fractions): +``` +Brat a(1, "34534534"); +Brat b("-SomeNumbersAreHere_b62", "-SomeNumbersAreHere_b62"); // base 62 +Brat c = "-9:10:79:100:16:3:35:72:76:15:11_b101 / 2"; // base 101 + +a += b; +a *= c; + +std::cout << "a = " << a << std::endl; +std::cout << "a b62 = " << a.base(62) << std::endl; +std::cout << "a point = " << a.point(50) << std::endl; +std::cout << "a point b62 = " << a.point(50, 62) << std::endl; +``` + +Output: +``` +a = -8481309383972525015147919680/1364114093 +a b62 = -b1SuMDAel1Q1KSPK_b62/1ujGod_b62 +a point = -6217448692521150439.53872535792356189666607307751053342427421135073633 +a point b62 = -7phYORB5xyv.xoRkTP8foonNxN1XMIa36GVhBAE59zioI0aAFYKQVLym6m6QrT_b62 +``` + [travis-shield]: https://img.shields.io/travis/tigertv/BeeNum/master.svg?style=for-the-badge&logo=travis [travis-link]: https://travis-ci.org/tigertv/BeeNum [github-shield]: https://img.shields.io/github/workflow/status/tigertv/BeeNum/Release.svg?style=for-the-badge&logo=github diff --git a/src/Brat/io.cpp b/src/Brat/io.cpp index 847e566..f62f61b 100644 --- a/src/Brat/io.cpp +++ b/src/Brat/io.cpp @@ -48,10 +48,16 @@ std::string Brat::point(const uint64_t num, const uint64_t base) const { Bint a = numerator; Bint t = a / denominator; a %= denominator; - std::string s = t.base(base); + + if (a < 0) { + a = -a; + } + + std::string s = (t == 0 && numerator < 0) ? "-" : ""; + s += t.base(base); s = s.substr(0, s.find('_')) + '.'; - uint64_t i; + uint64_t i; // get zeros for (i = num; i != 0; --i) { a *= base; @@ -59,6 +65,7 @@ std::string Brat::point(const uint64_t num, const uint64_t base) const { a %= denominator; if (t != 0) break; s += '0'; // TODO: replace, it's wrong for alphabet beginning from not '0' + if (base > 62) s += ':'; // TODO: replace, alphabet's size specific } // get rest diff --git a/tests/brat-point.cpp b/tests/brat-point.cpp index d8971ca..b7168d6 100644 --- a/tests/brat-point.cpp +++ b/tests/brat-point.cpp @@ -12,10 +12,12 @@ int main() { Brat c; std::vector> data = { - {"453453/434538", "1.04352898940944175192963561299587147729312511218811", "1.1kevzj90iucwsuh3jpribijw1mmg61qt71c5kztpvp2duatfet_b36"}, - {"171770057/8920503206", "0.01925564657433967632610253892890086810647574134171", "0.0oye3bbxc9ql9aq5wf6pcs85eirfkx3mojty0nze25udix3o6q_b36"}, - {"3648111/3648112", "0.99999972588560877516918340226396558000412268044402", "0.zzzzjfb96azavcnmk4nog2c7q7zyq5wghfp6i9805vzpdrcw1e_b36"}, - {"364811100000/3648112", "99999.97258856087751691834022639655800041226804440214554", "255r.z0h3b3ihxp0vodkay768y1nrfhr9lcttlmjblmude4q62lh9mu_b36"}, + {"453453/434538", "1.04352898940944175192963561299587147729312511218811", "1.1kevzj90iucwsuh3jpribijw1mmg61qt71c5kztpvp2duatfet_b36", "1.4:35:28:98:94:9:44:17:51:92:96:35:61:29:95:87:14:77:29:31:25:11:21:88:11:70:34:64:36:90:54:2:97:97:16:38:84:40:13:64:20:75:3:14:12:67:27:69:70:2_b100"}, + {"1/434538", "0.00000230129470840294749826252249515577463881179551", "0.0003v5f1av76ta9ao42vpvdgxl6wtt9gb1hb47vgdangvz7nk9_b36", "0.0:0:2:30:12:94:70:84:2:94:74:98:26:25:22:49:51:55:77:46:38:81:17:95:51:61:57:39:1:47:69:70:94:38:53:1:17:4:38:48:86:93:73:90:97:61:63:18:94:10_b100"}, + {"-1/434538", "-0.00000230129470840294749826252249515577463881179551", "-0.0003v5f1av76ta9ao42vpvdgxl6wtt9gb1hb47vgdangvz7nk9_b36", "-0.0:0:2:30:12:94:70:84:2:94:74:98:26:25:22:49:51:55:77:46:38:81:17:95:51:61:57:39:1:47:69:70:94:38:53:1:17:4:38:48:86:93:73:90:97:61:63:18:94:10_b100"}, + {"171770057/8920503206", "0.01925564657433967632610253892890086810647574134171", "0.0oye3bbxc9ql9aq5wf6pcs85eirfkx3mojty0nze25udix3o6q_b36", "0.1:92:55:64:65:74:33:96:76:32:61:2:53:89:28:90:8:68:10:64:75:74:13:41:71:55:15:77:37:85:82:24:58:67:98:4:66:3:38:12:66:69:57:74:78:54:20:77:66:31_b100"}, + {"3648111/3648112", "0.99999972588560877516918340226396558000412268044402", "0.zzzzjfb96azavcnmk4nog2c7q7zyq5wghfp6i9805vzpdrcw1e_b36", "0.99:99:99:72:58:85:60:87:75:16:91:83:40:22:63:96:55:80:0:41:22:68:4:44:2:14:55:48:16:29:94:99:57:67:67:37:99:48:86:12:19:17:30:95:56:28:55:52:63:65_b100"}, + {"364811100000/3648112", "99999.97258856087751691834022639655800041226804440214554", "255r.z0h3b3ihxp0vodkay768y1nrfhr9lcttlmjblmude4q62lh9mu_b36", "9:99:99.97:25:88:56:8:77:51:69:18:34:2:26:39:65:58:0:4:12:26:80:44:40:21:45:54:81:62:99:49:95:76:76:73:79:94:88:61:21:91:73:9:55:62:85:55:26:36:54:18:60_b100"}, }; /////////////////////////////////////////////////////////// @@ -46,4 +48,17 @@ int main() { std::cout << std::endl; } + std::cout << "Base100:" << std::endl; + b = 100; + for (auto& s : data) { + a = s[0]; + std::cout << "a = " << a << std::endl; + std::cout << "a.point = " << a.point(p) << std::endl; + std::cout << "a.point = " << a.point(p, b) << std::endl; + std::cout << "s = " << s[1] << std::endl; + std::cout << "s = " << s[3] << std::endl; + assert(a.point(p, b) == s[3]); + std::cout << std::endl; + } + } diff --git a/tests/brat.cpp b/tests/brat.cpp index 7753ea0..21866db 100644 --- a/tests/brat.cpp +++ b/tests/brat.cpp @@ -4,63 +4,20 @@ #include #include #include -#include - using namespace BeeNum; int main() { - Brat a("15", "98724"); - Brat b("34", "33339"); - Brat c = a + b; - std::cout << "a = " << a << std::endl; - std::cout << "b = " << b << std::endl; - std::cout << "c = " << c << std::endl; - std::cout << "a = " << a.point(10) << std::endl; - std::cout << "b = " << b.point(10) << std::endl; - std::cout << "c = " << c.point(50) << std::endl; - std::cout << std::endl; + Brat a(1, "34534534"); + Brat b("-SomeNumbersAreHere_b62", "-SomeNumbersAreHere_b62"); // base 62 + Brat c = "-9:10:79:100:16:3:35:72:76:15:11_b101 / 2"; // base 101 - Brat d("1", "3"); - std::cout << "d = " << d << std::endl; - d = 7 / d; - std::cout << "d = " << d << std::endl; - //d = d / 5; - //std::cout << "d = " << d << std::endl; -/* - Bint a; - Bint b; + a += b; + a *= c; - uint64_t d = 4000; - std::cout << "a = " << d << std::endl; - std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); - a = Math::fact(d); - std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); - - //assert(a.toString()=="6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000"); // 120! - //assert(a.toString()==s); - - //std::cout << "a = " << a << std::endl; - std::cout << "a b1000 = " << a.base(1000) << std::endl; - //std::cout << "s = " << "6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000" << std::endl; - / * - std::cout << "a bin = " << a.bin() << std::endl; - std::cout << "a hex = " << a.hex() << std::endl; - std::cout << "a oct = " << a.oct() << std::endl; - std::cout << "a b12 = " << a.base(12) << std::endl; - std::cout << "a b16 = " << a.base(16) << std::endl; + std::cout << "a = " << a << std::endl; std::cout << "a b62 = " << a.base(62) << std::endl; - std::cout << "a b100 = " << a.base(100) << std::endl; - std::cout << "a b101 = " << a.base(101) << std::endl; - std::cout << "a b1001 = " << a.base(1001) << std::endl; - - std::cout << "Time difference = " << std::chrono::duration_cast(end - begin).count() << "[µs]" << std::endl; - std::cout << "Time difference = " << std::chrono::duration_cast (end - begin).count() << "[ns]" << std::endl; - std::cout << std::endl; - -//*/ - - - + std::cout << "a point = " << a.point(50) << std::endl; + std::cout << "a point b62 = " << a.point(50, 62) << std::endl; } From 6dac54cd5391671a718984d820b0ed82c1609e97 Mon Sep 17 00:00:00 2001 From: tigertv Date: Tue, 15 Dec 2020 13:39:30 +0200 Subject: [PATCH 14/19] Make BeeNum libraries --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe0dba7..8e73e19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,9 +28,9 @@ add_library(shareobjects OBJECT ${BINT_SRC} ${MATH_SRC}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) -add_library(Bint SHARED ${BINT_SRC} ${MATH_SRC}) -add_library(Bint_static STATIC $) -set_target_properties(Bint_static PROPERTIES OUTPUT_NAME Bint) +add_library(BeeNum SHARED ${BINT_SRC} ${MATH_SRC}) +add_library(BeeNum_static STATIC $) +set_target_properties(BeeNum_static PROPERTIES OUTPUT_NAME BeeNum) # tests set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") From 8785452f29f2a71665f37438665f12be28f253af Mon Sep 17 00:00:00 2001 From: tigertv Date: Tue, 15 Dec 2020 13:46:42 +0200 Subject: [PATCH 15/19] Update Readme Code highlighting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c8174a..35f8768 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Time difference = 15621[µs] (g++ -Ofast) ``` Rationals(fractions): -``` +```cpp Brat a(1, "34534534"); Brat b("-SomeNumbersAreHere_b62", "-SomeNumbersAreHere_b62"); // base 62 Brat c = "-9:10:79:100:16:3:35:72:76:15:11_b101 / 2"; // base 101 From f0f51e15f087af1bf5ea2789fe76b087963365f7 Mon Sep 17 00:00:00 2001 From: tigertv Date: Wed, 16 Dec 2020 23:54:13 +0200 Subject: [PATCH 16/19] Update Bint's base test --- src/Bint/io.cpp | 7 ++++++- tests/base.cpp | 38 ++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Bint/io.cpp b/src/Bint/io.cpp index 6e1937a..a1ff0a5 100644 --- a/src/Bint/io.cpp +++ b/src/Bint/io.cpp @@ -120,7 +120,12 @@ std::string Bint::base(const uint64_t base) const { current = res; } - if (s.size() == 0) return "0"; + if (s.size() == 0) { + s = "0"; + if (base == 10) return s; + return s + "_b" + std::to_string(base); + } + if (isBaseBig) s.pop_back(); if (neg) s += '-'; diff --git a/tests/base.cpp b/tests/base.cpp index b146447..6c45a14 100644 --- a/tests/base.cpp +++ b/tests/base.cpp @@ -12,22 +12,23 @@ int main() { Bint c; std::vector> data = { - {"453453a_b32", "4466021482"}, - {"-453453a_b32", "-4466021482"}, - {"cc453453a_b32", "13610922415210"}, - {"cc45A3453a_b50", "23909464394262660"}, - {"0_b150", "0"}, - {"1_b150", "1"}, - {"1:12_b150", "162"}, - {"1:12:20_b150", "24320"}, - {"1:12:20:111_b150", "3648111"}, - {"1:1_b150", "151"}, - {"1:1:1_b150", "22651"}, + {"453453a_b32", "4466021482", "21uyc8a_b36"}, + {"-453453a_b32", "-4466021482", "-21uyc8a_b36"}, + {"cc453453a_b32", "13610922415210", "4torrs60a_b36"}, + {"cc45A3453a_b50", "23909464394262660", "6jf74b49m6c_b36"}, + {"0_b150", "0", "0_b36"}, + {"1_b150", "1", "1_b36"}, + {"1:12_b150", "162", "4i_b36"}, + {"1:12:20_b150", "24320", "irk_b36"}, + {"1:12:20:111_b150", "3648111", "266wf_b36"}, + {"1:1_b150", "151", "47_b36"}, + {"1:1:1_b150", "22651", "hh7_b36"}, }; /////////////////////////////////////////////////////////// std::cout << "Different Bases:" << std::endl; + std::cout << "From base to decimal:" << std::endl; for (auto& s : data) { a = s[0]; std::cout << "a = " << a << std::endl; @@ -37,12 +38,17 @@ int main() { std::cout << std::endl; } + std::cout << "From decimal to base62:" << std::endl; + int base = 36; + for (auto& s : data) { + a = s[1]; + std::cout << "a = " << a << std::endl; + std::cout << "a = " << a.base(base) << std::endl; + std::cout << "s = " << s[2] << std::endl; + assert(a.base(base)==s[2]); + std::cout << std::endl; + } - Bint d; - d = "333333"; - d = "acc45A3453a_b50"; - std::cout << "d = " << d << std::endl; - //*/ } From 3f4e6c203d687d5ed31a6027bc9a132f5e8a58a8 Mon Sep 17 00:00:00 2001 From: tigertv Date: Sat, 19 Dec 2020 22:42:02 +0200 Subject: [PATCH 17/19] Simplify oddFact Used less operations --- src/Math.cpp | 4 ++-- tests/factorial.cpp | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Math.cpp b/src/Math.cpp index 2466ddd..a508adc 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -111,8 +111,8 @@ Bint Math::oddFact(const uint64_t a, const uint64_t begin) { Bint last(n + 2); last *= (begin - 2); - for (uint64_t m = n - begin + 2; m >= 2; m -= 4) { - last += m << 1; + for (uint64_t m = (n - begin + 2) * 2; m >= 4; m -= 8) { + last += m; ret *= last; } diff --git a/tests/factorial.cpp b/tests/factorial.cpp index 229b9d6..782730c 100644 --- a/tests/factorial.cpp +++ b/tests/factorial.cpp @@ -20,12 +20,17 @@ std::string s = "182880195151406501331474317557391904421737771073043921970645269 std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); a = Math::fact(d); std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + std::cout << "a = " << a << std::endl; - assert(a.toString()==s); + assert(a.toString() == s); - //std::cout << "a b1000 = " << a.base(1000) << std::endl; std::cout << "Time difference = " << std::chrono::duration_cast(end - begin).count() << "[µs]" << std::endl; std::cout << "Time difference = " << std::chrono::duration_cast (end - begin).count() << "[ns]" << std::endl; std::cout << std::endl; + a = Math::fact(22); + std::cout << "a = fact(22) " << std::endl; + std::cout << "a = " << a << std::endl; + assert(a.toString() == "1124000727777607680000"); + } From a107b3f4174094b5a8fa5bc5d6cc4821900907da Mon Sep 17 00:00:00 2001 From: Sergey Larin Date: Tue, 12 Jul 2022 02:54:13 +0300 Subject: [PATCH 18/19] Fix addition with negative int64_t Negative part wasn't always handled, so expressions like (1 << 256) - 1 behaved incorrectly. --- src/Bint/arithmetics.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Bint/arithmetics.cpp b/src/Bint/arithmetics.cpp index 5e39075..f0daac2 100644 --- a/src/Bint/arithmetics.cpp +++ b/src/Bint/arithmetics.cpp @@ -81,16 +81,13 @@ Bint& Bint::operator += (const int64_t a) { bool carry = false; addUintWithCarry(number[0], a, carry); - if (carry) { - if (negA) { - for(int j = 1; j < (int)number.size(); j++) { - addUintWithCarry(number[j], -1, carry); - } - } else { - for(int j = 1; j < (int)number.size(); j++) { - addUintWithCarry(number[j], 0, carry); - if (!carry) break; - } + if (negA) { + for(int j = 1; j < (int)number.size(); j++) { + addUintWithCarry(number[j], -1, carry); + } + } else { + for(int j = 1; carry && j < (int)number.size(); j++) { + addUintWithCarry(number[j], 0, carry); } } From 94a22ccd56245137c41a96ab9ea9069702bb0e14 Mon Sep 17 00:00:00 2001 From: tigertv Date: Thu, 14 Jul 2022 21:28:15 +0200 Subject: [PATCH 19/19] Update subtruct test Checked (1 << 64) - 1 --- tests/subtract.cpp | 62 ++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/tests/subtract.cpp b/tests/subtract.cpp index c8afa28..9623423 100644 --- a/tests/subtract.cpp +++ b/tests/subtract.cpp @@ -8,53 +8,39 @@ using namespace BeeNum; int main() { - Bint a; - Bint b; - Bint c; - std::map data = { - {61, "2305843009213693952"}, {62, "4611686018427387904"}, {63, "9223372036854775808"}, - {64, "18446744073709551616"}, {65, "36893488147419103232"} + std::string data[][3] = { + {"-500000000000000000000", "500000000000000000000", "-1000000000000000000000"}, + {"500000000000000000000", "-500000000000000000000", "1000000000000000000000"}, + {"18446744073709551616", "1", "18446744073709551615"}, // (1 << 64) - 1 + {"115792089237316195423570985008687907853269984665640564039457584007913129639936", "1", "115792089237316195423570985008687907853269984665640564039457584007913129639935"}, // (1 << 256) - 1 }; /////////////////////////////////////////////////////////// - std::cout << "subtraction:" << std::endl; - std::string s = "5000000000000000000"; - - a = "-" + s; - std::cout << "a = " << a << std::endl; - std::cout << "a = " << a.bin() << std::endl; - - b = s; - std::cout << "b = " << b << std::endl; - std::cout << "b = " << b.bin() << std::endl; - - c = a - b; - std::cout << "c = " << c << std::endl; - std::cout << "c = " << c.bin() << std::endl; - assert(c.toString()=="-10000000000000000000"); - std::cout << std::endl; - - /////////////////////////////////////////////////////////// - - s = "50000000000000000000"; + Bint a; + Bint b; + Bint c; + std::string s; - b = s; - std::cout << "b = " << b << std::endl; - std::cout << "b = " << b.bin() << std::endl; + for (auto& item: data) { + a = item[0]; + std::cout << "a = " << a << std::endl; + std::cout << "a = " << a.hex() << std::endl; - a = "-" + s; - std::cout << "a = " << a << std::endl; - std::cout << "a = " << a.bin() << std::endl; + b = item[1]; + std::cout << "b = " << b << std::endl; + std::cout << "b = " << b.hex() << std::endl; - c = b - a; - std::cout << "c = " << c << std::endl; - std::cout << "c = " << c.bin() << std::endl; - assert(c.toString()=="100000000000000000000"); - std::cout << std::endl; + s = item[2]; + std::cout << "s = " << s << std::endl; - /////////////////////////////////////////////////////////// + c = a - b; + std::cout << "c = " << c << std::endl; + std::cout << "c = " << c.hex() << std::endl; + std::cout << std::endl; + assert(c.toString() == s); + } }