faheel / bigint Goto Github PK
View Code? Open in Web Editor NEWArbitrary-sized integer class for C++
Home Page: https://faheel.github.io/BigInt
License: MIT License
Arbitrary-sized integer class for C++
Home Page: https://faheel.github.io/BigInt
License: MIT License
Successive operations on different data can be threaded to speed things up.
For example, in operator*(BigInt, BigInt)
, the operations for stripping leading zeroes from, and adding trailing zeroes to different BigInt
objects can be multi-threaded and can be run concurrently.
Would it be useful if I implemented the ability to test if a negative number is valid or not.
I think it would be best to insert it into the existing function.
[ 3%] Building CXX object CMakeFiles/OperatorsRelationalTest.dir/test/operators/relational.cpp.o
[ 5%] Linking CXX executable ../bin/OperatorsRelationalTest
[ 5%] Built target OperatorsRelationalTest
Scanning dependencies of target OperatorsRelationalTest-capture-init
[ 7%] Capturing initial coverage data for test/operators/relational.cpp
geninfo: ERROR: /home/travis/build/faheel/BigInt/build/CMakeFiles/OperatorsRelationalTest.dir/test/operators/relational.cpp.gcno: reached unexpected end of file
make[3]: *** [CMakeFiles/OperatorsRelationalTest.dir/test/operators/relational.cpp.info.init] Error 255
make[3]: *** Deleting file `CMakeFiles/OperatorsRelationalTest.dir/test/operators/relational.cpp.info.init'
make[2]: *** [CMakeFiles/OperatorsRelationalTest-capture-init.dir/all] Error 2
make[1]: *** [all] Error 2
make: *** [coverage] Error 2
geninfo: ERROR: /home/travis/build/faheel/BigInt/build/CMakeFiles/OperatorsRelationalTest.dir/test/operators/relational.cpp.gcno: reached unexpected end of file
The following doesn't seem to work:
BigInt num = "12345678901234567890";
Generates the following error (using g++ version 7.x):
error: invalid conversion from ‘const char*’ to ‘const long long int&’ [-fpermissive]
BigInt num = "12345678901234567890";
^~~~~~~~~~~~~~~~~~~~~~
Add support for log2(x)
, log10(x)
, PROBABLY loge(x)
(though it might be tricky), and then implement log(a,b)
using the formula log_a(b) = log2(b)/log2(a)
I assume that author has written right implementation and forgot about description and not otherwise. So he meant to generate positive BigInt
s.
Then this:
Returns a random BigInt with a specific number of digits
should be changed to
Returns a random positive BigInt with a specific number of digits
This should be fixed in include/functions/random.hpp
and README.md
.
std::random_device
is used to generate random digits, but it is not intended to be used that way for general purposes(read this). So I suggest to use the modern way - std::mt19937
. Properly seeded it has 2^19937-1 potential states(that's a lot).
When we are using %
operation to achieve uniform integer distribution, that's not the right way, because some numbers are more likely to come up(read this for example). So let's use the right and modern way - std::uniform_int_distribution
to generate
BigInt
number.BigInt
numberBitwise operators are useful for performing bit-level operations on integral data types like char
or int
that are internally stored in binary as multiplying or dividing a number efficiently by a power of 2 is as simple as shifting bits to the left or to the right.
The BigInt
class, however, stores a number in its decimal form in a string, and so multiplying or dividing a number efficiently by a power of 10 is as simple as appending 0s or dropping digits from the end. And so supporting bitwise operators that work on powers of 2 wouldn't be very useful here as the number would first need to be converted to binary form, then the bitwise operation would be performed, and then the result would need to be converted to its decimal form, which isn't very efficient.
Therefore BigInt
wouldn't be supporting bitwise operators, unless some good use cases come up for using them on large numbers (thousands of decimal digits in size).
cout << sqrt(BigInt(121));
this code prints 1
, however sqrt from 121
is 11
.
In the Development section in README, add sample command(s) for properly generating project files using CMake for some popular Windows IDE, such as Visual Studio.
if I write set(CMAKE_CXX_STANDARD 20) in my cmake list, it says that "at line 272, to_string is not a member of std"
at line 395, it is stoi that is not a member of std
if i change the c++ version to 14 or 17, it says that there is no "or" "and"
Hi, I am trying to set up this project for development in CLion (on Windows 10, 64bit) but am having issues,
Currently, I have forked and pulled changes. In Clion, CMake Generation path is set as follows:
When trying to Run any test, say "ConstructorsTest" for example, I get the following errors and the build fails:
Add details regarding how and when to create a new header file for new features, how to write tests for it, and how to compile and run the tests using CMake and CTest.
Performing binary operations on integers or strings with a BigInt
object as the second operand should return a BigInt
:
big_int_1 = 5 + big_int_2;
big_int_1 = "5" + big_int_2;
// similarly for all other binary arithmetic and relational operators
Note: type T
can be a BigInt
, std::string
or long long
.
pow
BigInt pow(const T& base, int exponent);
Returns the value of baseexponent.
sqrt
BigInt sqrt(const BigInt& num);
Returns the square root of num
.
gcd
BigInt gcd(const T& num1, const BigInt& num2);
BigInt gcd(const BigInt& num1, const T& num2);
Returns the GCD of num1
and num2
.
lcm
BigInt lcm(const T& num1, const BigInt& num2);
BigInt lcm(const BigInt& num1, const T& num2);
Returns the LCM of num1
and num2
.
is_probable_prime
bool BigInt::is_probable_prime(size_t certainty);
Returns true
with a certainty of
for whether this BigInt is prime, based on either the Fermat or Miller-Rabin primality tests. Similar to Java's BigInteger.isProbablePrime
.
The README states that you only have to #include
a single header file. That's not true. You also have to include one or more header files from the directories below the one with BigInt.hpp depending one the needs of your code.
Thank you very much for such a nice library!
I had to integrate it in a project running on Windows. A couple of places had to be fixed since MSVC complained -- nothing big, mostly comparisons between a signed and unsigned value. Would you like me to create a pull request?
I also saw that suffix increments are written instead of commonly-used prefix increments (i++
instead of ++i
). Would you like me to fix that as well?
The following modulo computation causes an invalid string access in the library:
#include "BigInt.hpp"
#include <iostream>
int main() {
BigInt number("12345678901234567890123456789012345678901234567889");
std::cout << number % 4 << std::endl;
return 0;
}
how to create bigInt object from hexString ?
example :
i have 512 bit number b hex
std::string i = "66676466FFFFFFF6676466676466676466677a7863787a637a783635343635343634323334323334323334323335333435333134"
BigInt a(i);
a += randomNumber;
string hexVar = a.toHex();
i want create bigInt object from this, and export this object to hex
The following functions that generate a random BigInt
need to be implemented under include/functions/random.hpp
:
Having specific number of digits:
friend BigInt big_random(size_t num_digits);
Returns a random BigInt
having the specified number of digits. If the number of digits are not specified, use a random value for it.
Within a certain range:
friend BigInt big_random(const T& low, const T& high);
Returns a random BigInt
such that low <= BigInt <= high
.
Note: type T
can be a BigInt
, std::string
or long long
.
There weren't any includes in math.hpp file, after adding them abs function is failing, I'm working on it.
https://gmplib.org/manual/Installing-GMP
I am learning c++, and doing an course assignment which require big integer
I ‘ve done a test for this BigInt library and GNU MP Bignum Library
this library is nearly O(n^2), while the gmp is only O(n) .
It seems the karatsuba algortithm in this library does not work well.
Why don't we add bitwise operators here?
For some reasons, I can't run tests from command line, I'm not sure if it's an error with my specific configuration or with the test themselves.
Whenever I attempt to compile test by running "make" or run them using "make test", I get an error that "lcov is not found".
Here's the full error:
~/BigInt $ make
CMake Error at CMake/modules/CodeCoverage.cmake:115 (MESSAGE): lcov not found! Aborting...
Call Stack (most recent call first): CMakeLists.txt:84 (SETUP_TARGET_FOR_COVERAGE)
-- Configuring incomplete, errors occurred!
See also "~/BigInt/build/CMakeFiles/CMakeOutput.log".
Makefile:7: recipe for target 'default' failed
make: *** [default] Error 1
Use attaswift/BigInt as an inspiration for updating the README.
Also, make sure that the README is compatible to be used as the main page for the documentation generated by Doxygen (see #50).
BigInt header only does not work with hexdigit string like
std::string hexStr = "123456789abcdef123456789abcdef";
It is very kind of you to extend it.
Best thanks
Use Doxygen or other documentation generators for generating detailed and up-to-date documentation.
Calling sqrt() always returns 1, regardless of the number
big_random
might return a BigInt
whose value
has a leading '0'
, which will cause problems.
first * second
If any one of the operands is 10x, return the other operand appended by x zeroes.
first / second
If the second operand is 10x (and is less than the first operand), return the first size - x
digits of the first operand.
first % second
If the second operand is 10x (and is less than the first operand), return the last x digits of the first operand.
NOTE: in the implementation of the operators, the first operand is *this
, and the second operand is num
.
Instead of having the number's magnitude stored in a string, it would be much more efficient, in terms of both time and space, to have it stored in a vector of unsigned 64-bit integers (std::vector<uint64_t>
). That is, store a number's digits in base 264.
The following table lists how the number's magnitude will be represented in base 264:
Magnitude | Representation | Comment |
---|---|---|
0 | {0} |
0⋅(264)0 |
1 | {1} |
1⋅(264)0 |
2 | {2} |
2⋅(264)0 |
264 - 1 | {18446744073709551615} |
18446744073709551615⋅(264)0 |
264 | {0, 1} |
1⋅(264)1 |
264 + 1 | {1, 1} |
1⋅(264)0 + 1⋅(264)1 |
264 + 2 | {2, 1} |
2⋅(264)0 + 1⋅(264)1 |
265 | {0, 2} |
2⋅(264)1 |
550 | {11670147153572883801, 4814824860968089} |
11670147153572883801⋅(264)0 + 4814824860968089⋅(264)1 |
2128 | {0, 0, 1} |
1⋅(264)2 |
2192 | {0, 0, 0, 1} |
1⋅(264)3 |
2256 | {0, 0, 0, 0, 1} |
1⋅(264)4 |
The way how it could speed up performance is that, for example, when adding two BigInt
s, instead of adding their corresponding decimal digits together, their corresponding base 264 digits will be added, each digit in a single operation. Moreover, this addition (or any other arithmetic operation) of vectors can be vectorised by using SSE or AVX, which would further improve performance by a few folds.
Say, for instance, that you were adding two 5000 digit numbers. In base 10 they have 5000 digits, which means that it would take 5000 operations to add them together. Instead, if they were in base 264, they would have 260 digits, and would therefore require only 260 operations. Using AVX-512, vectors with up to 8 64-bit digits can be added together in a single operation. Which means that by using vectorized operations, two 5000 digit numbers could be added using just 33 operations.
a bug in (1453302010683093905187502545111768308832134376934/(-10531))
the result is -1380022880000293790256148755589380714920912959
but the right is -138002280000293790256148755589380714920912959
in function "std::tuple<BigInt, BigInt> divide(const BigInt& dividend, const BigInt& divisor) "
"while (temp < dividend) " may change to "while (temp <= dividend) "
There is no way of assigning a string literal to a BigInt directly. You can declare a BigInt and then assign a string literal but you can't do it at the same time.
For example, if I attempt:
BigInt a = "12345"
I get an error that says error: invalid conversion from ‘const char*’ to ‘const long long int&’
But this is legal:
BigInt a;
a = "12345"
I can also do:
BigInt a = (string) "12345"
I think the solution to this would simply to add a constructor and assignment for
BigInt(const char *)
BigInt& operator=(const char*)
When compiling on MacOS Big Sur a clang error is issued by the compiler on test/catch.hpp.
In file included from BigInt/test/test_runner.cpp:10:
BigInt/include/third_party/catch.hpp:5025:13: error: invalid token in expression
CATCH_BREAK_INTO_DEBUGGER();
I think it will be useful for the BigInt to take hex as input and represent it as BigInt and support all the operations? Let me know what you think of it and I can start working on it.
Wouldn't be redundant using overloaded operators like >> or << for I/O?, they could be used as bitwise operators in the future, especially considering you have a to_string() method which could serve as an out method, and one of yours constructors accepts a string as a parameter, you could just input a string and then call it.
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.