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
5
votes
1 answer

Implementing SHLD/SHRD instructions in C

I'm trying to efficiently implement SHLD and SHRD instructions of x86 without using inline assembly. uint32_t shld_UB_on_0(uint32_t a, uint32_t b, uint32_t c) { return a << c | b >> 32 - c; } seems to work, but invokes undefined behaviour when…
xiver77
  • 2,162
  • 1
  • 2
  • 12
5
votes
1 answer

Arbitrary precision integer on GPU

I'm currently doing a primality test on huge numbers (up to 10M digits). Right now, I'm using a c program using the GMP library. I did some parallelization using OpenMP and got a nice speedup (3.5~ with 4 cores). The problem is that I don't have…
Kyle
  • 87
  • 6
5
votes
2 answers

Why `+0n` throws an error but `-0n` does not?

The following code throws an error in javascript: console.log(String(+0n)) But this code runs successfully: console.log(String(-0n)) Why +0n throws an error but -0n does not?
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
5
votes
1 answer

How to Math.floor on BigInt in JavaScript?

Getting this error TypeError: Cannot convert a BigInt value to a number: function getCompositeNumbers() { let v = [] let i = 1n while (i < 1000000n) { if (checkIfDivisible(i)) { v.push(i) } i++ } return v } function…
Lance
  • 75,200
  • 93
  • 289
  • 503
5
votes
2 answers

Is it possible to declare a typescript function which works on both numbers and bigints?

In plain untyped Javascript, it's not so hard to write a function which can operate on either numbers or bigints, depending on the arguments which are passed in: const sumOfSquares = (a,b) => a*a + b*b; sumOfSquares(3, 4); // returns…
mlc
  • 1,668
  • 2
  • 16
  • 30
5
votes
3 answers

How do I get jq to preserve bigint values?

I have a large JSON file that contains bigints with their full values--not rounded like JavaScript loves to do by default. We have a workaround to deal with the bigints in Node.js, but I'm trying to use jq (the command-line tool) to clean up our…
Brandon
  • 695
  • 10
  • 29
5
votes
0 answers

How to bignum in Fortran

For a language designed for scientific computing---even 50ish years ago, I am surprised to find that Fortran 2018 didn't have a native type for bignum or bigInt types for arbitrary precision, and suspect I am doing something wrong. What is the…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
5
votes
1 answer

How to get the result of a Javascript BigInt division in significants and exponent

I would like to divide two BigInt numbers and get the result as some significant digits and an exponent. I have got this so far: f = (numerator, denominator, significantsLength) => { const denominatorLength = ('' + denominator).length …
Marco de Wit
  • 2,686
  • 18
  • 22
5
votes
2 answers

improving bigint write to disk performance

I am working with really large bigint numbers and I need to write them to disk and read them back later because they won't all fit in memory at a time. The current Chapel implementation first converts the bigint to a string and then writes that…
zx228
  • 93
  • 4
5
votes
1 answer

Rails 5.1.0 how to upgrade

Rails 5.1.0 introduces the bigint primary keys and foreign keys. All new tables will have bigint pk and creating a reference migration to an old table will not work since the old pk is a normal int. Using change_column _, :id,:bigint just errors…
Iaan Krynauw
  • 311
  • 3
  • 16
5
votes
4 answers

BigInts seem slow in Julia

I'm really impressed with Julia since it ran faster than D on a processor intensive Euler Project question. #303 if anyone is interested. What's weird is how slow BigInts in Julia seems to be. Strange because I read their performance is quite…
5
votes
1 answer

How to parse BigInt from the num crate?

I am trying to use BigInt. My code is like this: extern crate num; use num::bigint::BigInt; ... println!("{}", from_str::("1")); //this is line 91 in the code In my Cargo.toml file I have the following: [dependencies] num = "0.1.30" What I…
Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54
5
votes
2 answers

Java from BigInteger to BitSet and back

In Java 8 the below code converts integer 3 to bitset and prints {0, 1} meaning the bit representation of 3 has ones at positions 0 and 1 that is 11. System.out.println(BitSet.valueOf(new long[]{3})); I'm interested in converting a BigInteger or…
Stephan Rozinsky
  • 553
  • 2
  • 6
  • 21
5
votes
1 answer

Error converting data type varchar to bigint in stored procedure

I'm trying to call this procedure with the usp_TimesheetsAuditsLoadAllbyId 42747, NULL command. But I always get an error Msg 8114, Level 16, State 5, Procedure usp_TimesheetsAuditsLoadAllById, Line 9 Error converting data type varchar to…
davser
  • 85
  • 1
  • 1
  • 9
5
votes
1 answer

MySQL UUID_SHORT() gives error Out of range value for column

I'm using MySQL 5.6.17 on Amazon Web Services RDS and when calling SELECT UUID_SHORT() I'm getting a number bigger than 9223372036854775807. For example the number I get is 12057145185130250250 The problem is in my table I have a column as…
neildt
  • 5,101
  • 10
  • 56
  • 107