GithubHelp home page GithubHelp logo

michaeljclark / bignum Goto Github PK

View Code? Open in Web Editor NEW
25.0 6.0 3.0 82 KB

C++ bignum with support for arbitrary precision integer arithmetic.

License: MIT License

C++ 98.69% CMake 1.31%
cplusplus bitvector bignum bignumber bitwise-operators

bignum's Introduction

bignum

C++ bignum with support for arbitrary precision integer arithmetic.

  • wideint arbitrary fixed width signed two's complement or unsigned integer.
  • bignum arbitrary variable width signed two's complement or unsigned integer.
  • supports static or dynamic width.
  • supports arbitrary precision signed and unsigned arithmetic.
  • supports operator overloads for C++ math, logical and bitwise operators.
  • multiplication and division algorithm from Hacker's Delight.
  • divide and conquer algorithm for radix 10 conversion to string.

Build

The bignum library standard build process uses cmake.

GCC or Clang on Linux or macOS

mkdir build
cd build
cmake -G "Unix Makefiles" ..

Microsoft Visual Studio 2019 on Windows

mkdir build
cd build
cmake -G "Visual Studio 16 2019" ..

Examples

Example wideint arithmetic

#include <wideint.h>

typedef wideint<256> int256_t;

int main()
{

	int256_t a = 2;
	int256_t b = a | 3;
}

Example code for C++ bignum arithmetic

#include <iostream>

#include <bignum.h>

int main()
{
	bignum x = 2;
	bignum y = x.pow(4096);
	std::cout << y.to_string(10) << std::endl;
}

Equivalent code using GMP (GNU Multiple Precision Arithmetic Library)

#include <iostream>

#include <gmp.h>

int main()
{
	mpz_t result, base;

	mpz_init(base);
	mpz_init(result);
	mpz_set_ui(base, 2);
	mpz_pow_ui(result, base, 4096);

	std::cout << mpz_get_str(NULL, 10, result) << std::endl;

	mpz_clear(base);
	mpz_clear(result);
}

bignum API

bignum implements the following operators and methods:

  • bignum(unsigned int n)
  • bignum(std::string str, size_t radix)
  • bignum& operator+=(const bignum &operand)
  • bignum& operator-=(const bignum &operand)
  • bignum& operator<<=(int shamt)
  • bignum& operator>>=(int shamt)
  • bignum& operator&=(const bignum &operand)
  • bignum& operator|=(const bignum &operand)
  • bignum operator+(const bignum &operand) const
  • bignum operator-(const bignum &operand) const
  • bignum operator<<(int shamt) const
  • bignum operator>>(int shamt) const
  • bignum operator&(const bignum &operand) const
  • bignum operator|(const bignum &operand) const
  • bool operator==(const bignum &operand) const
  • bool operator<(const bignum &operand) const
  • bool operator!=(const bignum &operand) const
  • bool operator<=(const bignum &operand) const
  • bool operator>(const bignum &operand) const
  • bool operator>=(const bignum &operand) const
  • bool operator!() const
  • bignum operator*=(const bignum &operand)
  • bignum operator/=(const bignum &divisor)
  • bignum operator%=(const bignum &divisor)
  • bignum operator*(const bignum &operand) const
  • bignum operator/(const bignum &divisor) const
  • bignum operator%(const bignum &divisor) const
  • void from_string(std::string, size_t radix = 0 /autodetect/)
  • std::string to_string(size_t radix = 10) const

bignum's People

Contributors

michaeljclark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bignum's Issues

Any Benchmarks?

Hello,

I am seeking a fast arbitrary floating point and integer package for a Windows x64 project.

Bignum seems to be easy to use as compared to GMP/MPZ but I am wondering if it has any benchmarks against these or native calculations to get an idea of speed relevance as well.

Any thoughts?
Thanks and have a great day

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.