Coder Social home page Coder Social logo

arbitrary-precision-integers's Introduction

Arbitrary-Precision-Integers

Arbitrary Precision Integers C++ Class. Final Project for CSE701 - Foundations of Modern Scientific Programming Graduate Course.

In scientific Computing, We usually have to deal with large values which surpass the scope and the allocated space of conventional data types. While some programming languages handle this automatically such as Python, C++ doesn't include such functionality. C++ is one of the most known languages in Scientific Computing, providing the developer with control without sacrificing performance. Therefore, this project presents Arbitrary Precision Integers library for C++ to accommodate the scale of Scientific Computation using C++. The library allows the user to define arbitrary length integers and perform the well-known integer operations as addition, subtraction, negation, and comparison.

Class Implementation

The library currently contains one class - BigInt, which can take an input integer or string and convert them into an arbitrary precision integer by storing them into a vector and preserving the integer sign as a boolean.

Private Variables

The BigInt Class has two private variables:

  1. A vector of unsigned 8-bit integers to store the arbitrary precision integer.
    vector<uint8_t> bigint;
  1. A boolean variable to save the sign of the integer. It evaluates to True when the integer is negative and False when it's positive.
    bool sign_negative = false;

Public Functions

Constructors

The BigInt Class can be defined using four types of constructors

  1. Empty Constructor: when the user doesn't initialize the class object with a value. The empty constructor initializes the BigInt class with a vector of size 1 containing only the 0 digit and the sign as positive.
    BigInt();
  1. Constructor with int: takes a 64-bit integer as an input and converts it into a BigInt. The integer sign is saved in the sign_negative variable.
    BigInt(const int64_t number);
  1. Constructor with string: Takes a string of arbitrary length as input, converts the digits in the string to integers which are saved into the bigint vector to build BigInt. The function contains error checking and handling to prevent empty strings, strings with only the sign, and strings containing non-digit characters.
    BigInt(const string &number);
  1. Constructor of another BigInt instance: Takes a BigInt instances and builds another BigInt instance identical to it.
    BigInt(const BigInt &);

Arithmetic Operations

Includes defining and overloading arithmetic operations.

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Negation
    BigInt &operator-();
    BigInt &operator+=(const BigInt &);
    BigInt &operator-=(const BigInt &);
    BigInt &operator*=(const BigInt &);

Assignment

    BigInt &operator=(const BigInt &bigint);
    BigInt &operator=(const int64_t number);
    BigInt &operator=(const string &number);

Helper Functions

Class Non-member Functions

Insertion

ostream &operator<<(ostream &out, const BigInt &bigint);

Overloading Arithmetic Operations

BigInt operator+(BigInt, const BigInt &);
BigInt operator-(BigInt, const BigInt &);
BigInt operator*(BigInt , const BigInt &);

Overloading Comparison Operations

bool operator==(const BigInt &lhs, const BigInt &rhs);
bool operator!=(const BigInt &lhs, const BigInt &rhs);
bool operator>(const BigInt &lhs, const BigInt &rhs);
bool operator<(const BigInt &lhs, const BigInt &rhs);
bool operator>=(const BigInt &lhs, const BigInt &rhs);
bool operator<=(const BigInt &lhs, const BigInt &rhs);

How to run demos

in the main function in demo.cpp file run file_demo() to read input from file and perform operations. Run static_demo() to run the demo with the BigInts and operations defined inside it.

arbitrary-precision-integers's People

Contributors

nadaelmasry avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.