Questions tagged [bigint]

Arbitrary-precision arithmetic (also called bignum arithmetic, multiple precision arithmetic, or infinite-precision arithmetic) indicates that calculations are performed on numbers which digits of precision are limited only by the available memory of the host system. This contrasts with the faster fixed-precision arithmetic found in most arithmetic logic unit (ALU) hardware, which typically offers between 8 and 64 bits of precision.

Several modern programming languages have built-in support for bignums, and others have libraries available for arbitrary-precision integer and floating-point math. Rather than store values as a fixed number of binary bits related to the size of the processor register, these implementations typically use variable-length arrays of digits.

, , and , supports arbitrary precision integers (also known as infinite precision integers or bignums). Other languages which do not support this concept as a top-level construct may have libraries available to represent very large numbers using arrays of smaller variables, such as and class or "bigint" package.

These use as much of the computer's memory as is necessary to store the numbers; however, a computer has only a finite amount of storage, so they too can only represent a finite subset of the mathematical integers. These schemes support very large numbers, for example one kilobyte of memory could be used to store numbers up to 2466 decimal digits long.

Application

A common application is public-key cryptography (such as that in every modern Web browser), whose algorithms commonly employ arithmetic with integers having hundreds of digits. Another is in situations where artificial limits and overflows would be inappropriate. It is also useful for checking the results of fixed-precision calculations, and for determining the optimum value for coefficients needed in formulae, for example the √⅓ that appears in Gaussian integration.

Big ints can also be used to compute fundamental mathematical constants such as π to millions or more generally to investigate the precise behaviour of functions such as the Riemann zeta function where certain questions are difficult to explore via analytical methods. Another example is in rendering fractal images with an extremely high magnification.

Arbitrary-precision arithmetic can also be used to avoid overflow, which is an inherent limitation of fixed-precision arithmetic. Some processors can instead deal with overflow by saturation, which means that if a result would be unrepresentable, it is replaced with the nearest representable value.

718 questions
9
votes
1 answer

SQL Server varbinary bigint with BitConverter.ToInt64 values are different

I save my long value in a SQL Server table as varbinary(max): var savedValue = BitConverter.GetBytes(longValue); Now I need to work with that value in T-SQL query, but when I trying to get value: select cast(Value as bigint) from dbo.MyValues It…
9
votes
2 answers

Is there a generally accepted alternative to GMP for arbitrary precision?

In my quest for looking for a BigInt library, I came across this post: C or C++ BigInt library on Microsoft Windows The accepted answer mentions the GMP library, but one of the commenters claim that library does not error out very gracefully and…
Chad Harrison
  • 2,836
  • 4
  • 33
  • 50
9
votes
6 answers

rails3 bigint primary key

I would like to create a bigint (or string or whatever that is not int) typed primary key field under Rails 3. I have a given structure of data, for example: things ------ id bigint primary_key name char(32) The approach I'm currently trying to…
Notinlist
  • 16,144
  • 10
  • 57
  • 99
9
votes
1 answer

SQL bigint hash to match c# int64 hash

I am trying to create a universal hashing alogrithim that hashes a string as a 64 bit int. I am able to hash the strings correctly: sql: select convert ( varchar(64), HASHBYTES ( 'SHA1', …
Eulalie367
  • 198
  • 4
  • 17
8
votes
1 answer

Is there another way of testing if a big.Int is 0?

I'm working with big.Ints and need to test for 0. Right now, I'm using zero = big.NewInt(0)and Cmp(zero)==0 which works fine, but I was wondering if there's a quicker way specifically for 0 (I need this program to be very fast)?
lolad
  • 321
  • 6
  • 13
8
votes
2 answers

Is it possible to get the native CPU size of an integer in Rust?

For fun, I'm writing a bignum library in Rust. My goal (as with most bignum libraries) is to make it as efficient as I can. I'd like it to be efficient even on unusual architectures. It seems intuitive to me that a CPU will perform arithmetic faster…
Steven
  • 1,709
  • 3
  • 17
  • 27
8
votes
2 answers

json_decode AND json_encode long integers without losing data

As noted in the PHP documentation, when json_decodeing a data structure containing long integers, they'll be converted to floats. The workaround is to use JSON_BIGINT_AS_STRING, which preserves them as strings instead. When json_encodeing such…
SuperNova
  • 2,792
  • 2
  • 25
  • 34
8
votes
3 answers

Why does Perl warn about "useless constant 1" when using bigint?

I was writing a module as part of my application when I noticed syntax check results in warning about useless use of a constant (1). Why is that? The constant is the obligatory 1 at the end of the module which is normally ignored by warnings as…
Daniel Böhmer
  • 14,463
  • 5
  • 36
  • 46
8
votes
3 answers

Newton-Raphson Division With Big Integers

I'm making a BigInt class as a programming exercise. It uses a vector of 2's complement signed ints in base-65536 (so that 32-bit multiplications don't overflow. I will increase the base once I get it fully working). All of the basic math operations…
user3044553
  • 147
  • 2
  • 8
8
votes
3 answers

Understanding the use of BigInt

I'm struggling to understand how to use BigInt correctly. It seems to me that one should use BigInt when Int64 or Int128 is not enough, and apparently BigInt uses arbitrary precision arithmetic (of which I have no knowledge of). Let's say I want to…
kip820
  • 305
  • 3
  • 6
8
votes
6 answers

Fastest way to convert binary to decimal?

I've got four unsigned 32-bit integers representing an unsigned 128-bit integer, in little endian order: typedef struct { unsigned int part[4]; } bigint_t; I'd like to convert this number into its decimal string representation and output it to…
ianh
  • 836
  • 1
  • 5
  • 15
8
votes
5 answers

Convert Sqlite BigInt to Date

I have a Sqlite database that I am using as an ado.net job store for my Quartz.net scheduler jobs. In one table, a column called START_TIME is of type big int. Is there a way to cast or convert a bigint to a date value? I would like to be able to…
Mel
7
votes
2 answers

AddressSanitizer: stack-buffer-overflow on boost cpp_int

I have the following code sample that should convert std::array to BigInt i.e. boost::multiprecision::cpp_int. I compile this code with clang64 15.0.7 (С++17) from MSYS2. Code works fine and converts array of uint8_t to BigInt. #include…
mblw
  • 1,762
  • 1
  • 19
  • 28
7
votes
2 answers

In Javascript (but not Node), how do I divde two Uint8Arrays?

I'm using in-browser Javascript, not NodeJS. I have two Uint8Arrays ... var d1 = new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255]) var d2 = new Uint8Array([255, 255, 255, 255, 237, 49, 56, 0]) Each will have exactly 8 elements that are…
satish
  • 703
  • 5
  • 23
  • 52
7
votes
2 answers

How to use Math/Big in Go Lang

I am trying to create a factorial program, but when the numbers get too big the answer becomes wrong. Here is my code. I am new to math/big and cannot figure out how to correctly implement it into the program. Any help is appreciated.…
Brantley
  • 69
  • 1
  • 3