C++ class for creating and computing arbitrary-length integers

Overview

BigNumber

BigNumber build License

BigNumber is a C++ class that allows for the creation and computation of arbitrary-length integers.

The maximum possible length of a BigNumber is std::string::max_size.

Installation

To add BigNumber to your C++ project, you can download the bin folder from this repository, which contains the library and include files.

Then, simply include the header file in whichever file you need a BigNumber and link to the library file.

#include "bignumber.h"

Usage

BigNumber(string)

You can also use the = operator to set a BigNumber equal to an existing BigNumber, a number, or a string of numbers.

Examples:

BigNumber b("5");       //BigNumber b is created with value 5.
BigNumber c("-20");     //BigNumber c is created with value -20.
BigNumber d("0");       //BigNumber d is created with value 0.
BigNumber e = b;        //BigNumber e is created with value 5.
BigNumber f = 30;       //BigNumber f is created with value 30.
BigNumber g = "2060";   //BigNumber g is created with value 2060.
BigNumber h(22);        //BigNumber h is created with value 22.

Methods

add(BigNumber other)

Adds another BigNumber to the current instance

BigNumber("4").add(BigNumber("20")) => BigNumber("24")

subtract(BigNumber other)

Subtracts another BigNumber from the current instance

BigNumber("30").subtract(BigNumber("45")) => BigNumber("-15")

multiply(BigNumber other)

Multiplies the BigNumber by another BigNumber

BigNumber("12").multiply(BigNumber("4")) => BigNumber("48")

divide(BigNumber other)

Divides the BigNumber by another BigNumber

BigNumber("30").divide(BigNumber("5")) => BigNumber("6")

pow(int exponent)

Raises the BigNumber to the power of the exponent

BigNumber("2").pow(3) => BigNumber("8")

getString()

Returns the BigNumber as an std::string

BigNumber("3824").getString() => "3824"

setString(std::string newStr)

Sets the BigNumber's internal number string to a new string

BigNumber("2847").setString("38") => BigNumber("38")

negate()

Changes the sign of the BigNumber

BigNumber("3").negate() => BigNumber("-3")
BigNumber("-27").negate() => BigNumber("27")

equals(BigNumber other)

Checks if the other BigNumber is equal to this one

BigNumber("24").equals(BigNumber("28")) => false

digits()

Returns the number of digits in the BigNumber

BigNumber("28374").digits() => 5

isNegative()

Determines whether a BigNumber is negative

BigNumber("-278").isNegative() => true

isPositive()

Determines whether a BigNumber is positive

BigNumber("-3").isPositive() => false

isEven()

Determines whether a BigNumber is even

BigNumber("28472310").isEven() => true

isOdd()

Determines whether a BigNumber is odd

BigNumber("283427").isOdd() => true

abs()

Gets the absolute value of the BigNumber

BigNumber("-26").abs() => BigNumber("26")

Operator overloads

The following operators have been overloaded to work with BigNumbers:

<<

Output stream operator

std::cout << BigNumber("26") << std::endl => 26

+

Addition operator

BigNumber("2") + BigNumber("4") => BigNumber("6")

-

Subtraction operator

BigNumber("0") - BigNumber("2000") => BigNumber("-2000")

*

Multiplication operator

BigNumber("-20") * BigNumber("-5") => BigNumber("100")

/

Division operator

BigNumber("10") / BigNumber("-2") => BigNumber("-5")

==

Equal to operator

BigNumber("24") == BigNumber("24") => true

>

Greater than operator

BigNumber("2") > BigNumber("6") => false

<

Less than operator

BigNumber("2") < BigNumber("6") => true

>=

Greater than or equal to operator

BigNumber("34") >= BigNumber("22") => true

<=

Less than or equal to operator

BigNumber("383") <= BigNumber("383") => true

=

Assignment operator

BigNumber c("3") = BigNumber("8") => BigNumber("8")

+=

Adds and assigns to the BigNumber

BigNumber c("4") += BigNumber("3") => BigNumber("7")

-=

Subtracts and assigns to the BigNumber

BigNumber c("28") -= BigNumber("3") => BigNumber("25")

*=

Multiplies and assigns to the BigNumber

BigNumber c("3") *= BigNumber("4") => BigNumber("12")

/=

Divides and assigns to the BigNumber

BigNumber c("30") /= BigNumber("30") => BigNumer("1")

++ (Prefix)

Increments the BigNumber and returns the newly incremented number

++BigNumber("10") => BigNumber("11")

-- (Prefix)

Decrements the BigNumber and returns the newly decremented number

--BigNumber("34") => BigNumber("33")

++ (Postfix)

Increments the BigNumber but returns the original value

BigNumber("20")++ => BigNumber("20")

-- (Postfix)

Decrements the BigNumber but returns the original value

BigNumber("14")-- => BigNumber("14")

[]

Indexing operator

BigNumber d("26")[1] => 6

License

This project is under the Apache License.

Credit

The BigNumber class was created by Mark Guerra. Visit Limeoats.com for more information.

Issues
  • A question

    A question

    If I want to construct the class with a big integer without ""(quotation marks),like BigNumber a = 1234567898765432123456789; something like this. What should I do?

    question 
    opened by driftee 1
  • Maybe a bug when calculate bignumer divide

    Maybe a bug when calculate bignumer divide

    You could try this:

        BigNumber big256(hex2dec.Convert("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
        BigNumber target = big256 / BigNumber("1");
    

    it seems trap into an endless loop.

    opened by luoyhang003 1
Releases(1.0)
Owner
Limeoats
Founder of Limeoats. Independent game developer.
Limeoats
C++ tensors with broadcasting and lazy computing

Multi-dimensional arrays with broadcasting and lazy computing. Introduction xtensor is a C++ library meant for numerical analysis with multi-dimension

Xtensor Stack 2.7k Aug 6, 2022
Easy-to-use Scientific Computing library in/for C++ available for Linux and Windows.

Matrix Table of Contents Installation Development 2.1. Linux 2.2. Windows Benchmarking Testing Quick Start Guide 5.1. Initializers 5.2. Slicing 5.3. P

mlcpp 21 Jul 27, 2022
Kraken is an open-source modern math library that comes with a fast-fixed matrix class and math-related functions.

Kraken ?? Table of Contents Introduction Requirement Contents Installation Introduction Kraken is a modern math library written in a way that gives ac

yahya mohammed 24 Mar 28, 2022
Jing-Kalk is a beautifully designed calculator that conforms to the JingOS style and Integrating the interactive experience of pad and PC.

Jing-Kalk Jing-Kalk is based on Kalk gitlab. Jing-Kalk is a beautifully designed calculator that conforms to the JingOS style and Integrating the inte

JingOS 42 Jun 19, 2022
A toolkit for making real world machine learning and data analysis applications in C++

dlib C++ library Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real worl

Davis E. King 11.3k Jul 29, 2022
MIRACL Cryptographic SDK: Multiprecision Integer and Rational Arithmetic Cryptographic Library is a C software library that is widely regarded by developers as the gold standard open source SDK for elliptic curve cryptography (ECC).

MIRACL What is MIRACL? Multiprecision Integer and Rational Arithmetic Cryptographic Library – the MIRACL Crypto SDK – is a C software library that is

MIRACL 491 Aug 5, 2022
a lean linear math library, aimed at graphics programming. Supports vec3, vec4, mat4x4 and quaternions

linmath.h -- A small library for linear math as required for computer graphics linmath.h provides the most used types required for programming compute

datenwolf 692 Jul 28, 2022
tiny recursive descent expression parser, compiler, and evaluation engine for math expressions

TinyExpr TinyExpr is a very small recursive descent parser and evaluation engine for math expressions. It's handy when you want to add the ability to

Lewis Van Winkle 1.1k Aug 8, 2022
nml is a simple matrix and linear algebra library written in standard C.

nml is a simple matrix and linear algebra library written in standard C.

Andrei Ciobanu 35 Jul 5, 2022
Bolt is an algorithm for compressing vectors of real-valued data and running mathematical operations directly on the compressed representations.

Bolt is an algorithm for compressing vectors of real-valued data and running mathematical operations directly on the compressed representations.

null 2.3k Jul 29, 2022
C++ Mathematical Expression Parsing And Evaluation Library

C++ Mathematical Expression Toolkit Library Documentation Section 00 - Introduction Section 01 - Capabilities Section 02 - Example Expressions

Arash Partow 411 Jul 26, 2022
libmpc++ is a C++ header-only library to solve linear and non-linear MPC

libmpc++ libmpc++ is a C++ library to solve linear and non-linear MPC. The library is written in modern C++17 and it is tested to work on Linux, macOS

Nicola Piccinelli 39 Jul 8, 2022
A lightweight, minimal and customisable maths library for C99

Small Maths Library A lightweight, minimal and customisable maths library for C99, generated by Lua. Generating Requires Lua 5.3. lua sml.lua Generat

null 5 May 6, 2022
C++ Matrix -- High performance and accurate (e.g. edge cases) matrix math library with expression template arithmetic operators

Matrix This is a math and arithmetic matrix library. It has stood many years of performing in mission critical production for financial systems. It ha

Hossein Moein 69 Jul 29, 2022
Header only, single file, simple and efficient C++ library to compute the signed distance function to a triangle mesh

TriangleMeshDistance Header only, single file, simple and efficient C++11 library to compute the signed distance function to a triangle mesh. The dist

Interactive Computer Graphics 75 Jul 31, 2022
Library for nonconvex constrained optimization using the augmented Lagrangian method and the matrix-free PANOC algorithm.

alpaqa Alpaqa is an efficient implementation of the Augmented Lagrangian method for general nonlinear programming problems, which uses the first-order

OPTEC 12 Jul 28, 2022
A simple C++ complex & real matrix library, with matrix inversion, left division and determinant calculation

NaiveMatrixLib 帆帆的简易矩阵计算库 A simple C++ stdlib-based complex & real matrix library, with matrix inversion, left division (A\b) and determinant calculat

FerryYoungFan 48 Apr 27, 2022
A work-in-progress C++20/23 header-only maths library for game development, embedded, kernel and general-purpose that works in constant context.

kMath /kmæθ/ A work-in-progress general-purpose C++20/23 header-only maths library that works in constant context Abstract The kMath Project aims to p

The λ Project 10 Apr 18, 2022
Blitz++ is a C++ template class library which provides array objects for scientific computing

Blitz++ is a C++ template class library which provides array objects for scientific computing

Peter Kümmel 17 Nov 22, 2020
Given two arrays X and Y of positive integers, find the number of pairs such that x^y > y^x (raised to power of) where x is an element from X and y is an element from Y.

Number-of-pairs Given two arrays X and Y of positive integers, find the number of pairs such that x^y > y^x (raised to power of) where x is an element

Rajesh Kumar Sah 1 Nov 20, 2021
A simple C library for compressing lists of integers using binary packing

The SIMDComp library A simple C library for compressing lists of integers using binary packing and SIMD instructions. The assumption is either that yo

Daniel Lemire 391 Aug 7, 2022
Random access array of tightly packed unsigned integers

PackedArray: random access array of tightly packed unsigned integers TLDR PackedArray comes to the rescue when you're in a desperate need for an uint9

Gregory Pakosz 131 Jun 22, 2022
A C++20 implementation of safe (wrap around) integers following MISRA C++ rules

PSsimplesafeint A C++20 implementation of safe (wrap around) integers following MISRA C++ rules. I might also work on a C++17 implementation, but this

Peter Sommerlad 34 Jul 28, 2022
Safer integers in C++.

integers is a package for C++ that implements integer types with explicit, well-defined behavior (of your choice!) on overflow, underflow, division by 0, casting, and shifting too far.

Google 171 Jul 19, 2022
Two programs to find the LCM of two positive integers.

LCM-finders LCM-finders? LCM-finders is the repo for my LCM finder projects. I made this program in two similar languages. ?? Note: Two languages mean

Chandula Janith 1 Apr 15, 2022
C Program to input a string and adjust memory allocation according to the length of the string.

C-String C Program to input a string and adjust memory allocation according to the length of the string. With the help of this program, we have replic

Kunal Kumar Sahoo 1 Jan 20, 2022
COBS framing with implicit run-length-encoding, optimized for data containing statistically a bit more 0 and FF bytes in a row, as data often carry 16, 32 or 64 bit numbers with small values.

TCOBS Table of Contents About The project TCOBS Specification TCOBS code Getting Started 4.1. Prerequisites 4.2. Installation 4.3. Usage in Go 4.3.1.

Thomas Höhenleitner 8 Jul 30, 2022
TurboRLE-Fastest Run Length Encoding

TurboRLE: Turbo Run Length Encoding Efficient and fastest Run Length Encoding library ?? The fastest now up to 50% more faster incl. SSE/AVX2 + improv

powturbo 239 Aug 2, 2022
A collection of various algorithms to produce length-limited prefix codes

Introduction This is a collection of various algorithms to produce length-limited prefix codes. My library is written in plain C with tons of comments

Stephan Brumme 12 Mar 10, 2022