Questions tagged [arbitrary-precision]

All questions concerning numbers which support extremely high precision: Libraries in programming languages (GMP, MPFR), support of arbitrary precision in computer algebra systems (CAS, Mathematica, Maple, Mathlab) and how to correctly use and calculate numbers with very high precision and accuracy.

Sometimes the available precision of hardware-supported datatypes (most likely single precision (=32bit), double precision (=64bit), extended precision (=80bit)) is not enough to tackle some problems:

  • Problems given in natural sciences and applied and theoretical mathematics which needs utmost precision. These are mostly tackled by computer algebra systems like Mathematica and Maple.
  • Financial applications which must confirm to the exact rounding procedures given by law. They also should guarantee that the available range of numbers is big enough so that nonsensical results by leaving the supported range (overflow) will not occur.
  • Cryptological applications which need to handle very large integers because many algorithms (integer factorization, Diffie-Hellman key exchange) are based on them.

In this case speed and portability is sacrificed to support numbers with a much higher precision available than hardware-supported datatypes. They are called arbitrary-precision numbers.

As the handling of arbitrary-precision numbers and arithmetic is not standardized yet, there may be big differences between implementations. There are different packages supporting one or more arbitrary-precision datatypes: integer, binary and decimal float or rational.

The tag should be applied to all questions which need to use arbitrary-precision numbers: Problems which need high precision, the implementation of arbitrary-precision numbers, the programming of basic operations, known caveats of specific packages etc. Some programming languages have built-in capabilities to handle arbitrary-precision (BigDecimal in Java, Bignum in Ruby).

Free software:

Axiom: Free computer algebra system with arbitrary-precision capabilities.

GMP: GNU Multiple Precision Arithmetic Library

Important books:

Knuth, D. E. The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed. Reading, MA: Addison-Wesley, 1998.

Muller, Jean-Michel et al. Handbook of Floating-Point Arithmetic, Chapter 14, Birkhäuser Boston, 2009.

296 questions
5
votes
5 answers

Arbitrary precision arithmetic with Ruby

How the heck does Ruby do this? Does Jörg or anyone else know what's happening behind the scenes? Unfortunately I don't know C very well so bignum.c is of little help to me. I was just kind of curious it someone could explain (in plain English) the…
maček
  • 69,649
  • 33
  • 159
  • 193
5
votes
0 answers

Arbitrary precision arthimetic in Fortran

How can I do arbitrary precision arithmetic in Fortran? I need two things. I want to work with really big integer and real numbers, and to work with arbitrary precise real numbers.
MOON
  • 1,626
  • 3
  • 22
  • 34
5
votes
3 answers

Which programming languages have arbitrary precision floating-point literals?

This question is asking for programming languages which accept numeric constants for assignment to arbitrary precision variables or for use in arbitrary precision expressions without performing conversion to IEEE floating-point representation…
5
votes
2 answers

What is the fastest semi-arbitrary precision math library?

I'm using long double in a C program to compute 2D images of the Mandelbrot Set but wish to have further precision to zoom deeper. Are there any performance gains to be had from an arbitrary precision maths library that can restrict the amount of…
James Morris
  • 4,651
  • 3
  • 27
  • 49
5
votes
3 answers

Bignum implementation that has efficient addition of small integers

I have been using python's native bignums for an algorithm and decided to try and speed it up by converting it to C++. When I used long longs, the C++ was about 100x faster than the python, but when I used GMP bindings in C++, it was only 10x faster…
sligocki
  • 5,640
  • 4
  • 34
  • 41
5
votes
2 answers

Boost multiprecision fails because the implementation of complex tries to cast to double in internal functions like _Isinf or _Isnan

I need a BSD-like licensed C(++) multiprecision library with complex numbers support so I tried boost. The following code fails: #include #include using namespace boost::multiprecision; …
5
votes
3 answers

BigDecimal precision explosion

I'm trying to do calculations in Scala (and/or Java) at a fixed precision larger than that of a double. I'm using BigDecimals with Scala's default MathContext, which has precision 34. Even though all the inputs have this precision, I'm finding…
davidsd
  • 761
  • 4
  • 18
5
votes
2 answers

manually printing a N-byte integer

What is a scalable algorithm to print an N-binary-digit integer manually whose value does not fit in long long. I know printf and friends, along with (which most likely piggy-backs on have this builtin for standard types, but I'd…
rubenvb
  • 69,525
  • 30
  • 173
  • 306
4
votes
2 answers

MSVC++ "official" arbitrary precision library

I'm looking for an MSVC++ specific arbitrary precision library. Since I don't need cross platform compatibility for what I'm working, I'd rather not have the mess of it all. I tried looking at NTL but upon seeing statements like "These steps work…
bobobobo
  • 57,855
  • 58
  • 238
  • 337
4
votes
2 answers

when extending python with c, how do one cope with arbitrary size integers?

The Python/C API manual mentions conversion functions from⁽¹⁾ and to⁽²⁾ void pointers, which seem to be the only way to use arbitrary length python integers in C. (1) : PyLong_FromVoidPtr() and format 0& with Py_BuildValue() (2) :…
Camion
  • 792
  • 6
  • 16
4
votes
2 answers

Why do BigInteger implementations use sign-magnitude instead of two's complement?

Arbitary-precision signed integers are almost always implemented using a sign-magnitude representation: (Java) BigInteger in OpenJDK (Python) Bigint implementation of the Python built-in int type in CPython (C) mpz_t in GMP, the GNU multiple…
4
votes
3 answers

how to perform high precision calculations in D?

For some universitary work i have to approximate some numbers - like the Euler one with series. Therefore i have to add very small numbers, but i have problems with the precision. If the number ist very small it doesn't influence the result. real s;…
NaN
  • 3,301
  • 6
  • 39
  • 70
4
votes
2 answers

Why does this code return two different values doing the same?

#include #include #include int main() { double a; double b; double q0 = 0.5 * M_PI + 0.5 * -2.1500000405000002; double q1 = 0.5 * M_PI + 0.5 * 0.0000000000000000; double w0 = 0.5 * M_PI + 0.5 *…
Rafael
  • 392
  • 3
  • 14
4
votes
1 answer

CUDA implementation for arbitrary precision arithmetics

I have to multiply two very large (~ 2000 X 2000) dense matrices whose entries are floats with arbitrary precision (I am using GMP and the precision is currently set to 600). I was wondering if there is any CUDA library that supports arbitrary…
4
votes
1 answer

Find exact solutions to Linear Program

I need to find an exact real solution to a linear program (where all inputs are integers). It is important that the solver also outputs the solutions as rational numbers, ideally without doing any intermediate steps with floating point numbers. GLPK…
Manuel Eberl
  • 6,899
  • 12
  • 22