kasparsklavins / bigint Goto Github PK
View Code? Open in Web Editor NEWA lightweight big integer library for c++
License: MIT License
A lightweight big integer library for c++
License: MIT License
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?
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;
}
Bigint a("m");
The following code gives a loop.
Hi there!
You clearly need to handle power of zero since in your code you've got endless recursion.
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
Dodecahedron::Bigint a = 0;
Dodecahedron::Bigint b = "0";
if (a < b)
printf("Oops.\n");
Bigint a("-10");
Bigint b("-9");
Bigint c = a + b;
Bigint a("-10");
Bigint b("9");
Bigint c = a + b;
Bigint bi = 0;
if (bi == 0) // crash
bi = 1;
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.
regards
#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.
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;
Just a reminder to eventually add test coverage.
how to (XOR, OR, AND...)?
This class has addition ,subtraction and multiplication right now, but just out of curiosity, is there any reason that (integer) division and modulus are missing?
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 &);" ?
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];
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.