Giter Club home page Giter Club logo

bigint's Introduction

Build Status

Description

Bigint class provides math operations for arbitrarily large numbers. You know the limit is reached when your computer freezes.

Operators

Addition

Dodecahedron::Bigint a,b,c;
c = a + b;
c += a;
c = a + 6;
c += 6;

Subtraction

Dodecahedron::Bigint a,b,c;
c = a - b;
c -= a;

Multiplication

Dodecahedron::Bigint a,b,c;
c = a * b;
c *= a;
c = a * 6;
c *= 6;

Allocation

Dodecahedron::Bigint a = 12345;
Dodecahedron::Bigint b;
b = 159753;

Comparison

Dodecahedron::Bigint a = 159753;
Dodecahedron::Bigint b = 1634687496;

if(a == b) cout << "A is the same as B";
if(a < b) cout << "A is less than B";
if(a > b) cout << "A is larger than B";
if(a >= b) cout << "A is larger than B or equal to it";
if(a <= b) cout << "A is smaller than B or equal to it";

Access

Dodecahedron::Bigint a = 159753;
a.pow(15); //a^15, 1126510743106482...
cout << a[3]; // 6 is the 4th digit

Stream operators

Dodecahedron::Bigint a,b;
cin >> a >> b;
cout << a*b;

Methods

clear()

Clears the Dodecahedron::Bigint, essentially making it equal to 0.

Dodecahedron::Bigint a = 4558;
cout << a.pow(486);;  // ~1.46 * 10^1778
a.clear();
cout << a; //0

abs()

Absolute value.

Dodecahedron::Bigint a = -4558;
cout << a.abs() // 4558

pow(int)

Raises to the power of N.

Dodecahedron::Bigint a = 4558;
cout << a.pow(486); // ~1.46 * 10^1778

digits()

Returns the number of digits.

Dodecahedron::Bigint a = 4558;
cout << a.pow(486).digits(); // 4558^486 = 1779 digit number

trailing_zeros()

Returns the number of trailing zeros.

Dodecahedron::Bigint a = 4558;
a.pow(486);
cout << a.trailing_zeros(); //972

Functions

abs(Bigint)

Same as abs, but returns a new instance;

Dodecahedron::Bigint a = -455897864531248;
cout << abs(a) // 455897864531248

to_string(Bigint)

Converts the big integer to a string.

string str;
Dodecahedron::Bigint a = 455897864531248;
str = to_string(a);

factorial(int)

Returns a factorial of an integer, aka n!

cout << Dodecahedron::factorial(20000); //70`000+ digit number

bigint's People

Contributors

benlau avatar biancama avatar deins avatar kasparsklavins avatar mahmoudrizk avatar munsuri avatar nazarov-yuriy avatar quill88 avatar ruihe774 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  avatar  avatar  avatar

bigint's Issues

Incorrect product sign for negative values

typedef long double ldbl;

const int imax = numeric_limits< int >::min();
ldbl xdbl = pow(static_cast< ldbl >(imax), 5);
Dodecahedron::Bigint ibig = imax;
Dodecahedron::Bigint xbig = ibig.pow(5);

imax = -2147483648
xdbl = -4.56719e+046
xbig = 45671926166590716193865151022383844364247891968

The sign is incorrect for xbig.

The same incorrect result is obtained with
Dodecahedron::Bigint xbig = ibig * ibig * ibig * ibig * ibig;

Performance issue in Bigint::pow function

In function Bigint::pow there are two indentical recruisive calls, that could be replaced by just one call
Consider changing

Bigint Bigint::pow(int const &power, std::map<int, Bigint> &lookup)
{
    if (power == 1) return *this;
    if (lookup.count(power)) return lookup[power];

    int closestPower = 1;
    while (closestPower < power) closestPower <<= 1;
    closestPower >>= 1;

    //here pow(power / 2, lookup) is called twice
    if (power == closestPower) lookup[power] = pow(power / 2, lookup) * pow(power / 2, lookup);
    else lookup[power] = pow(closestPower, lookup) * pow(power - closestPower, lookup);

    return lookup[power];
}

to

Bigint Bigint::pow(int const &power, std::map<int, Bigint> &lookup)
{
    if (power == 1) return *this;
    if (lookup.count(power)) return lookup[power];

    int closestPower = 1;
    while (closestPower < power) closestPower <<= 1;
    closestPower >>= 1;

    if (power == closestPower)
    {
        auto tmp = pow(power / 2, lookup);
        lookup[power] = tmp * tmp;
    }
    else
    {
        lookup[power] = pow(closestPower, lookup) * pow(power - closestPower, lookup);
    }

    return lookup[power];
}

subtraction considered fail

I write this code:

#include"bigint.h"
#include<iostream>
using namespace std;
using namespace Dodecahedron;
int main(){
	Bigint a,b;
	cin>>a>>b;
	a-=b;
	cout<<a<<'\n'<<-1%10;
	return 0;
}

I get this output after #9 #ab6c858

IN:
5 6
OUT:
-999999999
-1

Before #9 at #1b840c7, everything is right

IN:
5 6
OUT:
-1
-1

So may "subtraction fix" make subtraction fail?

Dead code found at line 78

In file bigint.cpp around line 73, I found this code:

Bigint &Bigint::operator+=(Bigint const &b)
{
    if (!b.positive) {
        return *this -= b;
    }
    if (!b.positive && positive) {
        positive = false;
    }

The first if make it returned when !b.positive, then the second if can't be true, right? So it should be dead code

operator* and operator*=

the current code is:

    //Multiplication
    Bigint operator*(Bigint const &);
    Bigint &operator*=(Bigint const &);
    Bigint operator*(long long const &);
    Bigint &operator*=(int const &);

why not be "Bigint &operator*=(long long const &);" ?

if(n==0){//do not work

get 0 to Bigint n from cin.
but n==0 is not true.
code is below.

int main() {
    Dodecahedron::Bigint n;
    while(cin >> n){
        //if(Dodecahedron::to_string(n)=="0"){//ok
        //if(n==n*0){//ok
        if(n==0){//do not work
            break;
        }
        cout << n << "\n";
    }
    return 0;
}

use bigint with a class

hi,

thank you for this source code,
i am trying to use bigint with a class, but its not working, please help me to solve this problem.

maht_test.zip

regards

Power of zero

Hi there!
You clearly need to handle power of zero since in your code you've got endless recursion.

Bug with big number multiplication

#include<iostream>
#include<Bigint.h>
using namespace std;
using namespace Dodecahedron;
int main()
{
    Bigint a("4");
    Bigint b("100000000000000000000000000");
    Bigint c;
    c=a*b;
    cout<< c <<endl;
}

Given Output : 0
Correct Answer : 400000000000000000000000000

It fails when multiplying smaller number with large number, but not vice versa.

Failed cases

Bigint a("-10");
Bigint b("-9");
Bigint c = a + b;

Bigint a("-10");
Bigint b("9");
Bigint c = a + b;

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.