Skip to content

Simple C++ Library for Large Integers and Fractions

License

Notifications You must be signed in to change notification settings

RoyalXXX/bigint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

bigint

Simple C++ Library for Large Integers and Fractions

bigint is a lightweight C++ library for working with large integers and fractions. It is designed with simplicity in mind, requiring only a single header and a single implementation file.

Features

  • Supports arbitrarily large integers
  • Fractional (rational) number support
  • Minimalistic and easy to integrate
  • No external dependencies
  • 43 functions

Usage

Simply include the provided files and start using big integers and big fractions effortlessly in your C++ projects.

Documentation

Declaring and Intializing Variables

#include <string> // std::string
#include "bigint.h"
 
int main() 
{
    BigInt a("1"); 
    BigInt b{"2"}; // uniform initialization
    BigInt c = "-3"; // conversion constructor, const char* to BigInt
    BigInt d = {"4"};
    std::string s{"5"};
    BigInt e = s; // conversion constructor, std::string to BigInt
    BigInt f(s);
    BigInt g; // 0
    const char* h = "6";
    BigInt i = h; // conversion constructor, const char* to BigInt
    BigInt j(h);
    BigInt k(a); // copy constructor

    BigFrac l(a, b); // 1/2
    BigFrac m{c, d}; // -3/4
    BigFrac n("1", "3"); 
    BigFrac o = {s, "3"}; // 5/3
    BigFrac p; // 0/1
    BigFrac q = {"5", "15"}; // 1/3 auto simplifying
}

Printing BigInt and BigFrac

#include <iostream>
#include "bigint.h"
 
int main() 
{
    BigInt a("42");
    std::cout << a << std::endl;
    BigFrac c("-1", "2");
    std::cout << c << std::endl;
    std::cout << BigInt("100") << std::endl;
    std::cout << BigFrac("2", "7") << std::endl;
}

Getting the values of member variables

const std::string& get_value() const
bool get_sign() const
const BigInt& get_numerator() const
const BigInt& get_denominator() const

Example

#include <iostream>
#include <string>
#include "bigint.h"
 
int main() 
{
    BigInt a("42");
    std::string value = a.get_value(); // value = "42"
    bool sign = a.get_sign(); // sign = false

    BigFrac b("3", "7");
    BigInt num = b.get_numerator(); // num = "3"
    BigInt den = b.get_denominator(); // den = "7"
}

Overloaded operators for basic operations

Operators +, - (both binary and unary), *, /, %, and ^ are overloaded for convenience. However, alternative representations are also provided: Add, Subtract, Minus, Multiply, Divide, Remainder, and Pow.

BigInt Functions

Basic operations

Add +

BigInt Add(const BigInt& x, const BigInt& y)

Subtract -

BigInt Subtract(const BigInt&, const BigInt&)

Minus -

BigInt Minus(const BigInt&)

Multiply *

BigInt Multiply(const BigInt&, const BigInt&)

Divide /

BigInt Divide(const BigInt&, const BigInt&)

Remainder %

BigInt Remainder(const BigInt&, const BigInt&)

Pow ^

BigInt Pow(const BigInt&, const int)

Advanced operations

Abs

BigInt Abs(const BigInt&)

Factorial

BigInt Factorial(const int)

GCD

BigInt GCD(const BigInt&, const BigInt&)

LCM

BigInt LCM(const BigInt&, const BigInt&)

ISqrt

BigInt ISqrt(const BigInt&)

Fibonacci

BigInt Fibonacci(const int)

Binomial

BigInt Binomial(const int, const int)

EvenQ

bool EvenQ(const BigInt&)

OddQ

bool OddQ(const BigInt&)

IntegerLength

std::size_t IntegerLength(const BigInt&)

Approx

std::string Approx(const BigInt&, const std::size_t = 10ull)

Comparison of numbers

EqualQ ==

bool EqualQ(const BigInt&, const BigInt&)

NotEqualQ !=

bool NotEqualQ(const BigInt&, const BigInt&)

GreaterQ >

bool GreaterQ(const BigInt&, const BigInt&)

LessQ <

bool LessQ(const BigInt&, const BigInt&)

GreaterEqualQ >=

bool GreaterEqualQ(const BigInt&, const BigInt&)

LessEqualQ <=

bool LessEqualQ(const BigInt&, const BigInt&)

BigFrac Functions

Basic operations

Add +

BigFrac Add(const BigFrac&, const BigFrac&)

Subtract -

BigFrac Subtract(const BigFrac&, const BigFrac&)

Minus -

BigFrac Minus(const BigFrac&)

Multiply *

BigFrac Multiply(const BigFrac&, const BigFrac&)

Divide /

BigFrac Divide(const BigFrac&, const BigFrac&)

Pow ^

BigFrac Pow(const BigFrac&, const int)

Advanced operations

Abs

BigFrac Abs(const BigFrac&)

Harmonic

BigFrac Harmonic(const int)

Approx

std::string Approx(const BigFrac&)

Comparison of fractions

EqualQ ==

bool EqualQ(const BigFrac&, const BigFrac&)

NotEqualQ !=

bool NotEqualQ(const BigFrac&, const BigFrac&)

GreaterQ >

bool GreaterQ(const BigFrac&, const BigFrac&)

LessQ <

bool LessQ(const BigFrac&, const BigFrac&)

GreaterEqualQ >=

bool GreaterEqualQ(const BigFrac&, const BigFrac&)

LessEqualQ <=

bool LessEqualQ(const BigFrac&, const BigFrac&)

About

Simple C++ Library for Large Integers and Fractions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages