Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
tigertv committed Jul 14, 2022
2 parents 2f867ee + 94a22cc commit 80ee776
Show file tree
Hide file tree
Showing 42 changed files with 1,576 additions and 160 deletions.
12 changes: 0 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ 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})

# libraries
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 $<TARGET_OBJECTS:shareobjects>)
set_target_properties(Bint_static PROPERTIES OUTPUT_NAME Bint)
add_library(BeeNum SHARED ${BINT_SRC} ${MATH_SRC})
add_library(BeeNum_static STATIC $<TARGET_OBJECTS:shareobjects>)
set_target_properties(BeeNum_static PROPERTIES OUTPUT_NAME BeeNum)

# tests
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
Expand Down
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions include/Bint/Bint.h → include/BeeNum/Bint.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,7 +33,7 @@
#include <iostream>


namespace TigerTV {
namespace BeeNum {


class Bint {
Expand Down
126 changes: 126 additions & 0 deletions include/BeeNum/Brat.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#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
10 changes: 7 additions & 3 deletions include/Bint/Math.h → include/BeeNum/Math.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
};

Expand Down
6 changes: 3 additions & 3 deletions src/Bint.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,11 +23,11 @@
* SOFTWARE.
*/

#include <Bint/Bint.h>
#include <BeeNum/Bint.h>
#include <stdexcept>


namespace TigerTV {
namespace BeeNum {

const std::string Bint::alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

Expand Down
24 changes: 11 additions & 13 deletions src/Bint/arithmetics.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,9 +23,10 @@
* SOFTWARE.
*/

#include <Bint/Bint.h>
#include <BeeNum/Bint.h>

namespace TigerTV {

namespace BeeNum {

Bint Bint::operator + (const Bint& a) const {
Bint b = *this;
Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Bint/bitoperations.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,10 +23,10 @@
* SOFTWARE.
*/

#include <Bint/Bint.h>
#include <BeeNum/Bint.h>

namespace TigerTV {

namespace BeeNum {

Bint& Bint::bitOperation(const Bint& a, std::function<uint64_t(uint64_t&,const uint64_t&)>&& lambda) {
Bint aa(a);
Expand Down
7 changes: 4 additions & 3 deletions src/Bint/comparison.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,9 +23,10 @@
* SOFTWARE.
*/

#include <Bint/Bint.h>
#include <BeeNum/Bint.h>

namespace TigerTV {

namespace BeeNum {

bool Bint::operator == (const int64_t a) const {
if (number.size() != 1) return false;
Expand Down
Loading

0 comments on commit 80ee776

Please sign in to comment.