Coder Social home page Coder Social logo

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

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?

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;
}

Power of zero

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

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

Failed cases

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

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

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

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.

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;

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 &);" ?

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];
}

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.