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
12
votes
3 answers

Spreadsheet calculations with (at least) the accuracy of a C double

I was doing some calculations for planning an improved implementation of my prime sieve when I noticed that the Libre Office spreadsheet was showing the wrong values for numbers far below 2^53, which is the limit for precise integer calculations in…
11
votes
11 answers

What is the sum of the digits of the number 2^1000?

This is a problem from Project Euler, and this question includes some source code, so consider this your spoiler alert, in case you are interested in solving it yourself. It is discouraged to distribute solutions to the problems, and that isn't what…
travisbartley
  • 349
  • 2
  • 16
11
votes
3 answers

Speed up x64 assembler ADD loop

I'm working on arithmetic for multiplication of very long integers (some 100,000 decimal digits). As part of my library I to add two long numbers. Profiling shows that my code runs up to 25% of it's time in the add() and sub() routines, so it's…
cxxl
  • 4,014
  • 3
  • 25
  • 46
11
votes
3 answers

Is there an arbitrary precision floating point library for C/C++ which allows arbitrary precision exponents?

I'm looking for an arbitrary precision floating point library for C/C++ (plain C is preferred). I need arbitrary precision exponents. GMP and MPFR use fixed size exponents, so they are ineligible (I have some ideas for workarounds, but I prefer an…
user1743775
  • 111
  • 1
  • 3
11
votes
5 answers

Floats vs rationals in arbitrary precision fractional arithmetic (C/C++)

Since there are two ways of implementing an AP fractional number, one is to emulate the storage and behavior of the double data type, only with more bytes, and the other is to use an existing integer APA implementation for representing a fractional…
Desmond Hume
  • 6,732
  • 13
  • 57
  • 104
10
votes
2 answers

How can I set the level of precision for Raku's sqrt?

With Perl, one could use bignum to set the level of precision for all operators. As in: use bignum ( p => -50 ); print sqrt(20); # 4.47213595499957939281834733746255247088123671922305 With Raku I have no problems with rationals since I can use…
Julio
  • 4,491
  • 1
  • 8
  • 33
10
votes
1 answer

What's the best (for speed) arbitrary-precision library for C++?

I need the fastest library that is available for C++. My platform will be x86 and x86-64 which supports floating points.
Victor
  • 680
  • 1
  • 5
  • 16
10
votes
2 answers

JVM Arbitrary Precision Libraries

I'm working on a project ( in Scala ), where I have a need to manipulate some very large numbers; far too big to be represented by the integral types. Java provides the BigInteger and BigDecimal classes (and scala provides a nice thin wrapper…
nomad
  • 1,800
  • 1
  • 17
  • 33
9
votes
3 answers

Convert large hex string to decimal string

I need to convert a large (too large for the built-in data types) hex string to a string with it's decimal representation. For example: std::string sHex = "07AA17C660F3DD1D2A1B48F1B746C148"; std::string sDec; // should end up with:…
Flavio
  • 428
  • 3
  • 9
9
votes
2 answers

Converting an arbitrary-precision rational number (OCaml, zarith) to an approximate floating number

I'm using the Zarith library to do arbitrary-precision rational arithmetic. Suppose I have a rational number q of type Q.t that is the ratio of two big integers (Q is Zarith's arbitrary-precision rational number module). Sometimes, I would like to…
9
votes
3 answers

How to add 2 arbitrarily sized integers in C++?

I would like to add 2 arbitrarily sized integers in C++. How can I go about doing this?
BUKA
  • 91
  • 1
  • 2
9
votes
1 answer

Get GCC To Use Carry Logic For Arbitrary Precision Arithmetic Without Inline Assembly?

When working with arbitrary precision arithmetic (e.g. 512-bit integers), is there any way to get GCC to use ADC and similar instructions without using inline assembly? A first glance at GMP's sourcecode shows that they simply have assembly…
morrog
  • 634
  • 1
  • 8
  • 16
8
votes
0 answers

How should the MySQL Decimal datatype be used in php?

Hopefully just a quick question. I have a DECIMAL column in my database. The value is a very small decimal fraction - summing this value for all rows would equal 1. Now I'd like to use this value in my php application, display it, perform…
Bendos
  • 255
  • 2
  • 10
8
votes
6 answers

Python computing error

I’m using the API mpmath to compute the following sum Let us consider the serie u0, u1, u2 defined by: u0 = 3/2 = 1,5 u1 = 5/3 = 1,6666666… un+1 = 2003 - 6002/un + 4000/un un-1 The serie converges on 2, but with rounding problem it seems to…
8
votes
3 answers

How to generate random 64-bit value as decimal string in PHP

Oauth requires a random 64-bit, unsigned number encoded as an ASCII string in decimal format. Can you guys help me achieve this with php? Thanks
Julio Diaz
  • 7,923
  • 18
  • 47
  • 68
1
2
3
19 20