GithubHelp home page GithubHelp logo

limeoats / bignumber Goto Github PK

View Code? Open in Web Editor NEW
181.0 15.0 42.0 739 KB

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

License: Apache License 2.0

CMake 2.62% C++ 97.38%
bignumber biginteger bigint bignum

bignumber's Introduction

BigNumber

BigNumber build License

Buy Me a Coffee at ko-fi.com

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.

bignumber's People

Contributors

chamip avatar limeoats 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  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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bignumber's Issues

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.

gcc -> not comile & limited

I cant use it on Linux / Ubuntu.
Also view thats limited bignumber on long long.
I must to have extra type on number with over 100 digit in number.

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?

Floats?

Hey there!

can I calculate floating point numbers??

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.