Skip to content

Header-only library with arbitrarily large numbers

License

Notifications You must be signed in to change notification settings

admtrv/BigNumbers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BigNumbers

This is a C++ header-only library that implements classes to represent and operate on arbitrarily large numbers. It provides basic arithmetic, comparison, and additional operations suitable for high-precision computations.

BigInteger

Features

Constructors

  • Default Constructor: Initializes the value to 0.
  • Integer Constructor: Initializes from int64_t.
  • String Constructor: Initializes from a string representing an integer (supports -, +, and leading zeros).

Supported Operations

Arithmetic Operators
BigInteger a("1234567891011121314151617181920");
BigInteger b(-6789);

BigInteger sum = a + b;         // Addition
BigInteger diff = a - b;        // Subtraction
BigInteger prod = a * b;        // Multiplication
BigInteger quot = a / b;        // Division
BigInteger rem = a % b;         // Modulus
Compound Assignment Operators
a += b;
a -= b;
a *= b;
a /= b;
a %= b;
Unary Operators
BigInteger c = +a;  // Don't change sign
BigInteger d = -b;  // Change sign to opposite
Comparison Operators
if (a == b) { ... }
if (a != b) { ... }
if (a < b) { ... }
if (a > b) { ... }
if (a <= b) { ... }
if (a >= b) { ... }
Additional Operations
  • Square Root (Double Precision)
    double root = a.sqrt();
  • Integer Square Root (Only if defined)
    BigInteger root = a.isqrt();
  • Prime Check (Miller-Rabin)
    bool isPrime = a.is_prime(5); // 5 rounds of Miller-Rabin
Stream Input and Output
std::cout << a << std::endl;
std::cin >> a;
JSON Expression Evaluation

Supports arithmetic expressions in JSON format, e.g.:

std::string json = R"(
{
    "op":"+",
    "left": 123,
    "right": {
        "op":"*",
        "left": "12345678901234567890",
        "right": {
            "op":"%",
            "left":"34",
            "right":1
        }
    }
}
)";
BigInteger result = eval(json);   // Parse and evaluate expression
std::cout << result << std::endl; // 123

Note

To enable extra features like sqrt, isqrt, and is_prime, set the appropriate macros (SUPPORT_IFSTREAM, SUPPORT_MORE_OPS, SUPPORT_EVAL) to 1 before including the header.

BigRational

Features

Constructors

  • Default Constructor: Initializes the value to 0.
  • Fraction Constructor (Integer): Initializes from two int64_t values (numerator and denominator).
  • Fraction Constructor (String): Initializes from two strings representing the numerator and denominator.

Supported Operations

Arithmetic Operators
BigRational a("3", "4");    // 3/4
BigRational b(2, 5);        // 2/5

BigRational sum = a + b;    // Addition
BigRational diff = a - b;   // Subtraction
BigRational prod = a * b;   // Multiplication
BigRational quot = a / b;   // Division
Compound Assignment Operators
a += b;
a -= b;
a *= b;
a /= b;
Unary Operators
BigRational c = +a;
BigRational d = -b;
Comparison Operators
if (a == b) { ... }
if (a != b) { ... }
if (a < b) { ... }
if (a > b) { ... }
if (a <= b) { ... }
if (a >= b) { ... }
Additional Operations
  • Square Root (Double Precision)
    double root = a.sqrt();
  • Integer Square Root (Only if defined)
    BigInteger root = a.isqrt();
Stream Input and Output
std::cout << a << std::endl;
std::cin >> a;

Note

The BigRational class automatically reduces fractions to their simplest form upon creation or after any operation. The numerator and denominator are guaranteed to remain coprime.

BigRational a(6, 4);
std::сout << a << std::endl; // "3/2"

About

Header-only library with arbitrarily large numbers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published