Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Bignumber.js | 5,890 | 46,153 | 4,002 | 4 months ago | 54 | August 08, 2022 | 20 | mit | JavaScript | |
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic | ||||||||||
Decimal.js | 5,331 | 3,063 | 1,040 | a month ago | 50 | September 16, 2022 | 15 | mit | JavaScript | |
An arbitrary-precision Decimal type for JavaScript | ||||||||||
Decimal | 4,842 | 643 | 3,089 | 22 days ago | 28 | October 21, 2021 | 109 | other | Go | |
Arbitrary-precision fixed-point decimal numbers in go | ||||||||||
Big.js | 4,279 | 486,606 | 1,884 | 2 months ago | 33 | July 09, 2022 | 6 | mit | JavaScript | |
A small, fast JavaScript library for arbitrary-precision decimal arithmetic. | ||||||||||
Tiny Bignum C | 344 | 4 months ago | 12 | unlicense | C | |||||
Small portable multiple-precision unsigned integer arithmetic in C | ||||||||||
Decimal.js Light | 266 | 1,028 | 256 | a year ago | 20 | September 30, 2020 | 1 | mit | JavaScript | |
The light version of decimal.js, an arbitrary-precision Decimal type for JavaScript. | ||||||||||
Javascript Biginteger | 181 | 12 | 6 | 4 years ago | 4 | April 15, 2014 | 3 | mit | JavaScript | |
A big integer library for JavaScript | ||||||||||
Bcmath Extended | 67 | 18 | 11 | 2 months ago | 24 | February 09, 2022 | mit | PHP | ||
Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions. | ||||||||||
Nim Stint | 65 | a month ago | August 20, 2022 | 18 | apache-2.0 | Nim | ||||
Stack-based arbitrary-precision integers - Fast and portable with natural syntax for resource-restricted devices. | ||||||||||
Fermat | 61 | 3 | 5 months ago | 7 | June 27, 2021 | 4 | gpl-2.0 | PHP | ||
A library providing math and statistics operations for numbers of arbitrary size. |
Small portable Arbitrary-precision unsigned integer arithmetic in C, for calculating with large numbers.
Uses an array of uint8_t
, uint16_t
or uint32_t
as underlying data-type utilizing all bits in each word.
The number-base is 0x100, 0x10000 or 0x100000000 depending on chosen word-size - see the header file bn.h for clarification.
No dynamic memory management is utilized, and stdio.h
is only used for testing functions parsing to and from hex-strings.
Basic arithmetic (+, -, *, /, %) and bitwise operations (&, |, ^. <<, >>) plus increments, decrements and comparisons are supported.
The main design goal of this library is to be small, correct, self contained and use few resources while retaining acceptable performance and feature completeness. Clarity of the code is also highly valued.
malloc
/ free
).This is the data-structure used, where DTYPE is #define
'd to uint8_t
, uint16_t
or uint32_t
.
struct bn
{
DTYPE array[BN_ARRAY_SIZE];
};
This is the public / exported API:
/* Initialization functions: */
void bignum_init(struct bn* n); /* n gets zero-initialized */
void bignum_from_int(struct bn* n, DTYPE_TMP i);
int bignum_to_int(struct bn* n);
/* NOTE: The functions below are meant for testing mainly and expects input in hex-format and of a certain length */
/* See the implementation for details or the test-files for examples of how to use them. */
void bignum_from_string(struct bn* n, char* str, int nbytes);
void bignum_to_string(struct bn* n, char* str, int maxsize);
/* Basic arithmetic operations: */
void bignum_add(struct bn* a, struct bn* b, struct bn* c); /* c = a + b */
void bignum_sub(struct bn* a, struct bn* b, struct bn* c); /* c = a - b */
void bignum_mul(struct bn* a, struct bn* b, struct bn* c); /* c = a * b */
void bignum_div(struct bn* a, struct bn* b, struct bn* c); /* c = a / b */
void bignum_mod(struct bn* a, struct bn* b, struct bn* c); /* c = a % b */
void bignum_divmod(struct bn* a, struct bn* b, struct bn* c, struct bn* d); /* c = a/b, d = a%b */
/* Bitwise operations: */
void bignum_and(struct bn* a, struct bn* b, struct bn* c); /* c = a & b */
void bignum_or(struct bn* a, struct bn* b, struct bn* c); /* c = a | b */
void bignum_xor(struct bn* a, struct bn* b, struct bn* c); /* c = a ^ b */
void bignum_lshift(struct bn* a, struct bn* b, int nbits); /* b = a << nbits */
void bignum_rshift(struct bn* a, struct bn* b, int nbits); /* b = a >> nbits */
/* Special operators and comparison */
int bignum_cmp(struct bn* a, struct bn* b); /* Compare: returns LARGER, EQUAL or SMALLER */
int bignum_is_zero(struct bn* n); /* For comparison with zero */
void bignum_inc(struct bn* n); /* Increment: add one to n */
void bignum_dec(struct bn* n); /* Decrement: subtract one from n */
void bignum_pow(struct bn* a, struct bn* b, struct bn* c); /* Calculate a^b -- e.g. 2^10 => 1024 */
void bignum_isqrt(struct bn* a, struct bn* b); /* Integer square root -- e.g. isqrt(5) => 2 */
void bignum_assign(struct bn* dst, struct bn* src); /* Copy src into dst -- dst := src */
Set BN_ARRAY_SIZE
in bn.h
to determine the size of the numbers you want to use. Default choice is 1024 bit numbers.
Set WORD_SIZE
to {1,2,4} to useuint8_t
, uint16_t
or uint32_t
as underlying data structure.
Run make clean all test
for examples of usage and for some random testing.
See tests/factorial.c
for an example of how to calculate factorial(100) or 100! (a 150+ digit number).
Q: What differentiates this library from other C big integer implementations?
A: Small size for one. ~500 lines of C-code compiling to 2-3kb ROM, using only modest amounts of RAM. Utilizing all bits by using a number base 2^{8,16,32} instead of 10 which is a usual choice.
Q: Why no support for 64-bit word-size?
A: All calculations are done in a temporary variable, which needs to be bigger than the word-size (to detect overflow etc.). So 64-bit word-size would need e.g. 128-bit temp-var. C99 only supports portable integers up to 64-bits.
All material in this repository is in the public domain.