Giter VIP home page Giter VIP logo

little-big-int's People

Contributors

connormanning avatar pmed 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

Watchers

 avatar  avatar  avatar

little-big-int's Issues

There are overflows in BigUint for values in [18450000000000000000, 20479999999999999999].

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") = 20548786484558921568

x = 18449999999999999999
x + 1 = 18450000000000000000
y = 3255926290448384
z = 18446744073709551616
y + z = 18450000000000000000
y * 50 = 162796314522419200
375010160849369951
303763968758945934
303763968758945934

Not working on 32 bit code due to unmatching size of std::size_t and BigUint::Block

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.

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.