connormanning / little-big-int Goto Github PK
View Code? Open in Web Editor NEWA C++ big-integer library, highly optimized for the small-number case.
License: MIT License
A C++ big-integer library, highly optimized for the small-number case.
License: MIT License
Code example:
cout << "BigUint(\"18431029234558821566\") = " << BigUint("18431029234558821566") << endl;
cout << "BigUint(\"18449999999999999999\") = " << BigUint("18449999999999999999") << endl; // correct
uint64_t A(18449999999999999999);
uint64_t B(18450000000000000000);
cout << "A: " << A << ", B: " << B << endl; // A: 3255926290448383 (= 18449999999999999999 mod 2^64), B: 3255926290448384 (= 18450000000000000000 mod 2^64)
cout << "BigUint(\"18450000000000000000\") = " << BigUint("18450000000000000000") << endl; //BigUint("18450000000000000000") = 3255926290448384
cout << "BigUint(\"18470091734558821566\") = " << BigUint("18470091734558821566") << endl; //BigUint("18470091734558821566") = 23347660849269950
cout << "BigUint(\"18821754234558921567\") = " << BigUint("18821754234558921567") << endl;
cout << "BigUint(\"20479999999999999999\") = " << BigUint("20479999999999999999") << endl; //BigUint("20479999999999999999") = 2033255926290448383
uint64_t C(20479999999999999999);
uint64_t D(20480000000000000000);
cout << "C: " << C << ", D: " << D << endl; // C: 2033255926290448383 (= 20479999999999999999 mod 2^64), D: 2033255926290448384 (= 20480000000000000000 mod 2^64)
cout << "BigUint(\"20480000000000000000\") = " << BigUint("20480000000000000000") << endl; //correct
cout << "BigUint(\"20548786484558921568\") = " << BigUint("20548786484558921568") << endl;
BigUint x("18449999999999999999");
cout << "\nx = " << x << endl;
cout << "x + 1 = " << x + 1 << endl;
BigUint y("18450000000000000000"); // fail
cout << "y = " << y << endl;
BigUint z = x + 1 - y;
cout << "z = " << z << endl;
cout << "y + z = " << y + z << endl; // ok: 18450000000000000000
cout << "y * 50 = " << y * 50 << endl; // ok
//Operators also fail:
cout << BigUint("18821754234558921567") << endl; // fail: 375010160849369951
BigUint a("18821754234558921567");
cout << a * 50 << endl; // fail: 303763968758945934
cout << 50 * a << endl; // fail: 303763968758945934
Output:
BigUint("18431029234558821566") = 18431029234558821566
BigUint("18449999999999999999") = 18449999999999999999
A: 3255926290448383, B: 3255926290448384
BigUint("18450000000000000000") = 3255926290448384
BigUint("18470091734558821566") = 23347660849269950
BigUint("18821754234558921567") = 375010160849369951
BigUint("20479999999999999999") = 2033255926290448383
C: 2033255926290448383, D: 2033255926290448384
BigUint("20480000000000000000") = 20480000000000000000
BigUint("20548786484558921568") = 20548786484558921568x = 18449999999999999999
x + 1 = 18450000000000000000
y = 3255926290448384
z = 18446744073709551616
y + z = 18450000000000000000
y * 50 = 162796314522419200
375010160849369951
303763968758945934
303763968758945934
See connormanning/entwine#4. When a BigUint is moved, the movement of the underlying stack allocator is not handled correctly.
When compiled as 32 bit code, the std::size_t type has only 32 bit, but the code defines
using Block = unsigned long long
which has 64 bit. As a result, something like
const the std::size_t BigUint::blockMax = std::numeric_limits<Block>::max();
produces an overflow. Consequently, the class does not work correctly for instances of BigUint that exceed the unsigned long long size (i.e. 64 bit). The sample, for example, produces unhandled overflow exception due to the failure of std::ostream& operator<<(std::ostream& out, const BigUint& val);
.
I would recommend to not use the std::size_t
type, but BigUint::Block
(or some newly introduced BigUint::BlockSize
for code quality) instead. This has to be changed in several methods which use std::size_t
as a type to contain blocks.
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.