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: diff --git a/CMakeLists.txt b/CMakeLists.txt index 19f02de..8e73e19 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}) @@ -27,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") diff --git a/README.md b/README.md index 5037b6d..35f8768 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ -# Bint +# BeeNum [![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,7 +53,30 @@ 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 +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 + +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 +[github-link]: https://github.com/tigertv/BeeNum/actions diff --git a/include/Bint/Bint.h b/include/BeeNum/Bint.h similarity index 98% rename from include/Bint/Bint.h rename to include/BeeNum/Bint.h index ca5c018..6d33c53 100644 --- a/include/Bint/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 new file mode 100644 index 0000000..51536ef --- /dev/null +++ b/include/BeeNum/Brat.h @@ -0,0 +1,126 @@ +/* + * MIT License + * + * 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 + * 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 BeeNum { + +class Brat { +private: + Bint numerator; + Bint denominator; + + void simplify(); + +public: + // Constructors + Brat(); + Brat(const int64_t num); + Brat(const char* s); + 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; + 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 + 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/include/Bint/Math.h b/include/BeeNum/Math.h similarity index 84% rename from include/Bint/Math.h rename to include/BeeNum/Math.h index 96c66f9..b35a720 100644 --- a/include/Bint/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 @@ -28,20 +28,24 @@ #define TIGERTV_MATH_H #include "Bint.h" +#include "Brat.h" -namespace TigerTV { +namespace BeeNum { class Math { private: 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); + 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/Bint.cpp b/src/Bint.cpp index c8b1c22..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 @@ -23,11 +23,11 @@ * SOFTWARE. */ -#include +#include #include -namespace TigerTV { +namespace BeeNum { const std::string Bint::alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; diff --git a/src/Bint/arithmetics.cpp b/src/Bint/arithmetics.cpp index cad5c9b..f0daac2 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 @@ -23,9 +23,10 @@ * SOFTWARE. */ -#include +#include -namespace TigerTV { + +namespace BeeNum { Bint Bint::operator + (const Bint& a) const { Bint b = *this; @@ -80,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); } } diff --git a/src/Bint/bitoperations.cpp b/src/Bint/bitoperations.cpp index 003c260..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 @@ -23,10 +23,10 @@ * SOFTWARE. */ -#include +#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 ea87a8a..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 @@ -23,9 +23,10 @@ * SOFTWARE. */ -#include +#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 895f8e4..a1ff0a5 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 @@ -24,9 +24,10 @@ */ #include -#include +#include -namespace TigerTV { + +namespace BeeNum { Bint::operator std::string() const { return this->toString(); @@ -119,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/src/Bint/shifts.cpp b/src/Bint/shifts.cpp index ed0e550..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 @@ -23,10 +23,10 @@ * SOFTWARE. */ -#include +#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 new file mode 100644 index 0000000..61e0b1e --- /dev/null +++ b/src/Brat.cpp @@ -0,0 +1,83 @@ +/* + * MIT License + * + * 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 + * 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 BeeNum { + +Brat::Brat() { + //numerator = 0; + denominator = 1; +} + +Brat::Brat(const Bint& numerator, const Bint& denominator) : numerator(numerator), denominator(denominator) { + simplify(); +} + +Brat::Brat(const int64_t num) { + numerator = num; + denominator = 1; +} + +Brat::Brat(const std::string& s) { + auto end = s.find('/'); + numerator = s.substr(0, end); + if (end != std::string::npos) { + denominator = s.substr(end + 1); + } else { + denominator = 1; + } + simplify(); +} + +Brat::Brat(const char* s) : Brat((std::string)s) { + +} + +void Brat::simplify() { + if (denominator < 0) { + denominator = -denominator; + numerator = -numerator; + } + + Bint c = Math::gcd(numerator, denominator); + if (c != 1) { + numerator /= c; + denominator /= c; + } +} + +Bint Brat::getNumerator() const { + return numerator; +} + +Bint Brat::getDenominator() const { + return denominator; +} + + +} // namespace diff --git a/src/Brat/arithmetics.cpp b/src/Brat/arithmetics.cpp new file mode 100644 index 0000000..d6e8b0c --- /dev/null +++ b/src/Brat/arithmetics.cpp @@ -0,0 +1,194 @@ +/* + * MIT License + * + * 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 + * 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 BeeNum { + +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..60c7e32 --- /dev/null +++ b/src/Brat/comparison.cpp @@ -0,0 +1,144 @@ +/* + * MIT License + * + * 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 + * 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 BeeNum { + +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..f62f61b --- /dev/null +++ b/src/Brat/io.cpp @@ -0,0 +1,102 @@ +/* + * MIT License + * + * 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 + * 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 BeeNum { + +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 { + 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; + + 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; + // get zeros + for (i = num; i != 0; --i) { + a *= base; + t = a / denominator; + 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 + Bint b; + for ( ; i != 0; --i) { + b *= base; + b += t; + + a *= base; + t = a / denominator; + a %= denominator; + } + + return s + b.base(base); +} + +//////////////////////////////////////////////////////////////////////// +// 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/src/Math.cpp b/src/Math.cpp index cf9b235..a508adc 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 @@ -23,10 +23,10 @@ * SOFTWARE. */ -#include +#include -namespace TigerTV { +namespace BeeNum { Bint Math::pow(const Bint& a, uint64_t exp) { @@ -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; } @@ -189,5 +189,25 @@ 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; +} + +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/addition.cpp b/tests/addition.cpp index 84de287..d1c8083 100644 --- a/tests/addition.cpp +++ b/tests/addition.cpp @@ -2,10 +2,10 @@ #include #include #include -#include +#include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/base.cpp b/tests/base.cpp index 5283b8c..6c45a14 100644 --- a/tests/base.cpp +++ b/tests/base.cpp @@ -2,10 +2,9 @@ #include #include #include -#include -#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { @@ -13,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; @@ -38,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; - //*/ } diff --git a/tests/bits.cpp b/tests/bits.cpp index f89259c..596f6be 100644 --- a/tests/bits.cpp +++ b/tests/bits.cpp @@ -2,9 +2,9 @@ #include #include #include -#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/brat-addition.cpp b/tests/brat-addition.cpp new file mode 100644 index 0000000..50e823e --- /dev/null +++ b/tests/brat-addition.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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-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-comparison.cpp b/tests/brat-comparison.cpp new file mode 100644 index 0000000..d92af48 --- /dev/null +++ b/tests/brat-comparison.cpp @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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 new file mode 100644 index 0000000..3488134 --- /dev/null +++ b/tests/brat-division.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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 << "division:" << 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; + } + +} diff --git a/tests/brat-gcd.cpp b/tests/brat-gcd.cpp new file mode 100644 index 0000000..7a716fb --- /dev/null +++ b/tests/brat-gcd.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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..d0aaa70 --- /dev/null +++ b/tests/brat-lcm.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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; + } + + +} diff --git a/tests/brat-point.cpp b/tests/brat-point.cpp new file mode 100644 index 0000000..b7168d6 --- /dev/null +++ b/tests/brat-point.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +int main() { + Brat a; + Brat c; + + std::vector> data = { + {"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"}, + }; + + /////////////////////////////////////////////////////////// + + 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; + } + + 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-pows.cpp b/tests/brat-pows.cpp new file mode 100644 index 0000000..d87a8a7 --- /dev/null +++ b/tests/brat-pows.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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; + } + +} diff --git a/tests/brat-product.cpp b/tests/brat-product.cpp new file mode 100644 index 0000000..8179799 --- /dev/null +++ b/tests/brat-product.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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; + } +} diff --git a/tests/brat-subtract.cpp b/tests/brat-subtract.cpp new file mode 100644 index 0000000..2492376 --- /dev/null +++ b/tests/brat-subtract.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +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/brat.cpp b/tests/brat.cpp new file mode 100644 index 0000000..21866db --- /dev/null +++ b/tests/brat.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include + +using namespace BeeNum; + + +int main() { + 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; +} diff --git a/tests/comparison.cpp b/tests/comparison.cpp index fbadc7a..ff49c33 100644 --- a/tests/comparison.cpp +++ b/tests/comparison.cpp @@ -2,10 +2,9 @@ #include #include #include -#include -#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/division.cpp b/tests/division.cpp index a19f586..1fbf744 100644 --- a/tests/division.cpp +++ b/tests/division.cpp @@ -2,10 +2,9 @@ #include #include #include -#include -#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/factorial.cpp b/tests/factorial.cpp index 364883d..782730c 100644 --- a/tests/factorial.cpp +++ b/tests/factorial.cpp @@ -2,11 +2,11 @@ #include #include #include -#include -#include +#include +#include #include -using namespace TigerTV; +using namespace BeeNum; int main() { @@ -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"); + } diff --git a/tests/fibonnaci.cpp b/tests/fibonnaci.cpp index ab6cc0d..60aff2d 100644 --- a/tests/fibonnaci.cpp +++ b/tests/fibonnaci.cpp @@ -2,10 +2,10 @@ #include #include #include -#include -#include +#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/gcd.cpp b/tests/gcd.cpp index 11fa593..07a6754 100644 --- a/tests/gcd.cpp +++ b/tests/gcd.cpp @@ -2,10 +2,10 @@ #include #include #include -#include -#include +#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { @@ -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); diff --git a/tests/lcm.cpp b/tests/lcm.cpp index 6ff55fa..a50f3bc 100644 --- a/tests/lcm.cpp +++ b/tests/lcm.cpp @@ -2,10 +2,10 @@ #include #include #include -#include -#include +#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/main.cpp b/tests/main.cpp index 6e01bd7..4049e49 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -2,11 +2,11 @@ #include #include #include -#include -#include +#include +#include #include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/pows.cpp b/tests/pows.cpp index 334a66b..a5354b8 100644 --- a/tests/pows.cpp +++ b/tests/pows.cpp @@ -2,10 +2,10 @@ #include #include #include -#include -#include +#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/product.cpp b/tests/product.cpp index 1a15fa6..2deb87f 100644 --- a/tests/product.cpp +++ b/tests/product.cpp @@ -2,10 +2,9 @@ #include #include #include -#include -#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/shifts.cpp b/tests/shifts.cpp index c6d1589..92c9b96 100644 --- a/tests/shifts.cpp +++ b/tests/shifts.cpp @@ -2,10 +2,9 @@ #include #include #include -#include -#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { diff --git a/tests/subtract.cpp b/tests/subtract.cpp index 22bcc4b..9623423 100644 --- a/tests/subtract.cpp +++ b/tests/subtract.cpp @@ -2,61 +2,45 @@ #include #include #include -#include "Bint/Bint.h" -#include +#include -using namespace TigerTV; +using namespace BeeNum; int main() { - Bint a; - Bint b; - Bint c; - std::map data; - 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); + } }