All Projects → kokke → Tiny Bignum C

kokke / Tiny Bignum C

Licence: unlicense
Small portable multiple-precision unsigned integer arithmetic in C

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Tiny Bignum C

imath
Arbitrary precision integer and rational arithmetic library
Stars: ✭ 92 (-58.93%)
Mutual labels:  arbitrary-precision
Mpmath
Python library for arbitrary-precision floating-point arithmetic
Stars: ✭ 511 (+128.13%)
Mutual labels:  arbitrary-precision
Kotlin Multiplatform Bignum
A Kotlin multiplatform library for arbitrary precision arithmetics
Stars: ✭ 119 (-46.87%)
Mutual labels:  arbitrary-precision
Angourimath
Open-source symbolic algebra library for C# and F#. One of the most powerful in .NET
Stars: ✭ 266 (+18.75%)
Mutual labels:  arbitrary-precision
Decimal.js
An arbitrary-precision Decimal type for JavaScript
Stars: ✭ 4,585 (+1946.88%)
Mutual labels:  arbitrary-precision
Lua Bint
Arbitrary precision integer arithmetic library in pure Lua
Stars: ✭ 20 (-91.07%)
Mutual labels:  arbitrary-precision
bcmath-extended
Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions.
Stars: ✭ 59 (-73.66%)
Mutual labels:  arbitrary-precision
Big Math
Advanced Java BigDecimal math functions (pow, sqrt, log, sin, ...) using arbitrary precision.
Stars: ✭ 173 (-22.77%)
Mutual labels:  arbitrary-precision
Bignumber.js
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
Stars: ✭ 5,174 (+2209.82%)
Mutual labels:  arbitrary-precision
Physicalconstants.jl
Collection of fundamental physical constants with uncertainties. It supports arbitrary-precision constants
Stars: ✭ 55 (-75.45%)
Mutual labels:  arbitrary-precision
Arb
C library for arbitrary-precision ball arithmetic
Stars: ✭ 280 (+25%)
Mutual labels:  arbitrary-precision
Decimal
A high-performance, arbitrary-precision, floating-point decimal library.
Stars: ✭ 363 (+62.05%)
Mutual labels:  arbitrary-precision
Math
Arbitrary-precision arithmetic library for PHP
Stars: ✭ 905 (+304.02%)
Mutual labels:  arbitrary-precision
wide-integer
Wide-Integer implements a generic C++ template for uint128_t, uint256_t, uint512_t, uint1024_t, etc.
Stars: ✭ 83 (-62.95%)
Mutual labels:  arbitrary-precision
Calc
C-style arbitrary precision calculator
Stars: ✭ 127 (-43.3%)
Mutual labels:  arbitrary-precision
decimal
An object-oriented implementation of basic math operation with arbitrary precision, using BC Math if available.
Stars: ✭ 14 (-93.75%)
Mutual labels:  arbitrary-precision
Rpn
Math functional language, inspired by Hewlett-Packard Reverse Polish Notation (RPL) language
Stars: ✭ 17 (-92.41%)
Mutual labels:  arbitrary-precision
Javascript Biginteger
A big integer library for JavaScript
Stars: ✭ 181 (-19.2%)
Mutual labels:  arbitrary-precision
Swift Bigint
A lightweight, Multiple Precision Arithmetic Library for Swift!
Stars: ✭ 144 (-35.71%)
Mutual labels:  arbitrary-precision
Rationals
🔟 Implementation of rational number arithmetic for .NET with arbitrary precision.
Stars: ✭ 40 (-82.14%)
Mutual labels:  arbitrary-precision

CI

tiny-bignum-c

A small multiple-precision integer implementation in C

Description

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.

Current status

Basic arithmetic (+, -, *, /, %) and bitwise operations (&, |, ^. <<, >>) plus increments, decrements and comparisons are supported.

Design goals

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.

Notable features and omissions

  • Small code and binary size: ~500 SLOC, ~3kb binary for x86. Statically #define'd memory usage / allocation.
  • No use of dynamic memory allocation (i.e. no calls to malloc / free).
  • Randomized testing validated against Python's big-integers
  • Optimal memory utilization, number base is 1 + UINT{8,16,32}_MAX.

API

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 */

Usage

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_tas underlying data structure.

Run make clean all test for examples of usage and for some random testing.

Examples

See tests/factorial.c for an example of how to calculate factorial(100) or 100! (a 150+ digit number).

FAQ

  • 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.

License

All material in this repository is in the public domain.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].