Git Product home page Git Product logo

bigint's Introduction

BigInt

Arbitrary-sized integer class for C++


Release version Travis status Try it online License

๐Ÿšง Work in progress ๐Ÿšง

Contents

Highlights

  • No additional dependencies apart from the standard library.
  • Modern C++ (compiles with C++11 / C++14 / C++17).
  • No special compiling or linking required. Simply download the single header file, include it in your code, and compile however you would.

Usage

  1. Download the single-include header file to a location under your include path. Then #include it in your code:

    #include "BigInt.hpp"   // the actual path may vary
  2. Create objects of the BigInt class, and do what you got to do!

    BigInt big1 = 1234567890, big2;
    big2 = "9876543210123456789098765432101234567890";
    
    std::cout << big1 * big2 * 123456 << "\n";
    // Output: 1505331490682966620443288524512589666204282352096057600

Features

Operators

  • Assignment: =

    The second operand can either be a BigInt, an integer (up to long long) or a string (std::string or a string literal).

    big1 = 1234567890;
    big1 = "123456789012345678901234567890";
    big1 = big2;
  • Unary arithmetic: +, -

    big1 = +big2;   // doesn't return the absolute value
    big1 = -big2;
  • Binary arithmetic: +, -, *, /, %

    One of the operands has to be a BigInt and the other can be a BigInt, an integer (up to long long) or a string (std::string or a string literal).

    big1 = big2 + 1234567890;
    big1 = big2 - "123456789012345678901234567890";
    big1 = big2 * big3;
    big1 = 1234567890 / big2;
    big1 = "123456789012345678901234567890" % big2;
  • Arithmetic-assignment: +=, -=, *=, /=, %=

    The second operand can either be a BigInt, an integer (up to long long) or a string (std::string or a string literal).

    big1 += big2;
    big1 -= 1234567890;
    big1 *= "123456789012345678901234567890";
    big1 /= big2;
    big1 %= 1234567890;
  • Increment and decrement: ++, --

    big1 = ++big2;   // pre-increment
    big1 = --big2;   // pre-decrement
    
    big1 = big2++;   // post-increment
    big1 = big2--;   // post-decrement
  • Relational: <, >, <=, >=, ==, !=

    One of the operands has to be a BigInt and the other can be a BigInt, an integer (up to long long) or a string (std::string or a string literal).

    if (big1 < 1234567890
        or big1 > "123456789012345678901234567890"
        or big1 <= big2
        or 1234567890 >= big1
        or "123456789012345678901234567890" == big1
        or big1 != big3) {
        ...
    }
  • I/O stream: <<, >>

    std::cout << big1 << ", " << big2 << "\n";
    output_file << big1 << ", " << big2 << "\n";
    
    std::cin >> big1 >> big2;
    input_file >> big1 >> big2;

Functions

  • Conversion: to_string, to_int, to_long, to_long_long

    Convert a BigInt to either a string, int, long, or long long.

    Note: If the BigInt is beyond the range of the target type, an out_of_range exception is thrown.

    some_str = big1.to_string();
    
    some_int = big1.to_int();
    
    some_long = big1.to_long();
    
    some_long_long = big1.to_long_long();
  • Math

    • abs

      Get the absolute value of a BigInt.

      big1 = abs(big2);
    • big_pow10

      Get a BigInt equal to 10x.

      big1 = big_pow10(5000);   // big1 = 10^5000

Compiling / testing

Since this project is built as a library, there are no source files. However, there are unit tests for each header file that the project is split into.

  • To compile the tests, run make.
  • To build and run the tests, run make test.

You will need to run make at least once before you can run make test.

Contributing

Please read the contributing guidelines for details on how to contribute to the project.

License

This project is licensed under the terms of the MIT license.

bigint's People

Contributors

abelmarm avatar faheel avatar

Watchers

 avatar  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.