Arbitrary-Precision Numbers

When you do calculations with arbitrary-precision numbers, the Wolfram Language keeps track of precision at all points. In general, the Wolfram Language tries to give you results which have the highest possible precision, given the precision of the input you provided.

The Wolfram Language treats arbitrary-precision numbers as representing the values of quantities where a certain number of digits are known, and the rest are unknown. In general, an arbitrary-precision number x is taken to have (RefLink[Precision, paclet:ref/Precision])[x] digits which are known exactly, followed by an infinite number of digits which are completely unknown.

This computes π to 10-digit precision:

N[Pi,10]

After a certain point, all digits are indeterminate:

RealDigits[%,10,13]

When you do a computation, the Wolfram Language keeps track of which digits in your result could be affected by unknown digits in your input. It sets the precision of your result so that no affected digits are ever included. This procedure ensures that all digits returned by the Wolfram Language are correct, whatever the values of the unknown digits may be.

This evaluates Γ(1/7) to 30-digit precision:

N[Gamma[1/7],30]

The result has a precision of exactly 30 digits:

Precision[%]

If you give input only to a few digits of precision, the Wolfram Language cannot give you such high-precision output:

N[Gamma[0.142],30]

If you want the Wolfram Language to assume that the argument is exactly 142/1000, then you have to say so explicitly:

N[Gamma[142/1000],30]

In many computations, the precision of the results you get progressively degrades as a result of "roundoff error". A typical case of this occurs if you subtract two numbers that are close together. The result you get depends on high-order digits in each number, and typically has far fewer digits of precision than either of the original numbers.

Both input numbers have a precision of around 20 digits, but the result has much lower precision:

1.11111111111111111111-1.11111111111111111000

Adding extra digits in one number but not the other is not sufficient to allow extra digits to be found in the result:

1.11111111111111111111345-1.11111111111111111000

The precision of the output from a function can depend in a complicated way on the precision of the input. Functions that vary rapidly typically give less precise output, since the variation of the output associated with uncertainties in the input is larger. Functions that are close to constants can actually give output that is more precise than their input.

Here is a case where the output is less precise than the input:

Sin[111111111.0000000000000000]

Here is (e^(-40)) evaluated to 20-digit precision:

N[Exp[-40],20]

The result you get by adding the exact integer 1 has a higher precision:

1+%

It is worth realizing that different ways of doing the same calculation can end up giving you results with very different precisions. Typically, if you once lose precision in a calculation, it is essentially impossible to regain it; in losing precision, you are effectively losing information about your result.

Here is a 40-digit number that is close to 1:

x=N[1-10^-30,40]

Adding 1 to it gives another 40-digit number:

1+x

The original precision has been maintained:

Precision[%]

This way of computing 1+x loses precision:

(x^2-1)/(x-1)

The result obtained in this way has quite low precision:

Precision[%]

The fact that different ways of doing the same calculation can give you different numerical answers means, among other things, that comparisons between approximate real numbers must be treated with care. In testing whether two real numbers are "equal", the Wolfram Language effectively finds their difference, and tests whether the result is "consistent with zero" to the precision given.

These numbers are equal to the precision given:

3==3.000000000000000000

The internal algorithms that the Wolfram Language uses to evaluate mathematical functions are set up to maintain as much precision as possible. In most cases, built-in Wolfram Language functions will give you results that have as much precision as can be justified on the basis of your input. In some cases, however, it is simply impractical to do this, and the Wolfram Language will give you results that have lower precision. If you give higher-precision input, the Wolfram Language will use higher precision in its internal calculations, and you will usually be able to get a higher-precision result.

Numerical evaluation.

If you start with an expression that contains only integers and other exact numeric quantities, then (RefLink[N, paclet:ref/N])[expr,n] will in almost all cases succeed in giving you a result to n digits of precision. You should realize, however, that to do this the Wolfram Language sometimes has to perform internal intermediate calculations to much higher precision.

The global variable (RefLink[$MaxExtraPrecision, paclet:ref/$MaxExtraPrecision]) specifies how many additional digits should be allowed in such intermediate calculations.

Controlling precision in intermediate calculations.

The Wolfram Language automatically increases the precision that it uses internally in order to get the correct answer here:

N[Sin[10^40],30]

Using the default setting (RefLink[$MaxExtraPrecision, paclet:ref/$MaxExtraPrecision])=50, the Wolfram Language cannot get the correct answer here:

N[Sin[10^100],30]

This tells the Wolfram Language that it can use more digits in its internal calculations:

$MaxExtraPrecision=200

Now it gets the correct answer:

N[Sin[10^100],30]

This resets (RefLink[$MaxExtraPrecision, paclet:ref/$MaxExtraPrecision]) to its default value:

$MaxExtraPrecision=50

Even when you are doing computations that give exact results, the Wolfram Language still occasionally uses approximate numbers for some of its internal calculations, so that the value of (RefLink[$MaxExtraPrecision, paclet:ref/$MaxExtraPrecision]) can thus have an effect.

The Wolfram Language works this out using bounds from approximate numbers:

Sin[Exp[100]]>0

With the default value of (RefLink[$MaxExtraPrecision, paclet:ref/$MaxExtraPrecision]), the Wolfram Language cannot work this out:

Sin[Exp[200]]>0

Temporarily resetting (RefLink[$MaxExtraPrecision, paclet:ref/$MaxExtraPrecision]) allows the Wolfram Language to get the result:

Block[{$MaxExtraPrecision=100},Sin[Exp[200]]>0]

In doing calculations that degrade precision, it is possible to end up with numbers that have no significant digits at all. But even in such cases, the Wolfram Language still maintains information on the accuracy of the numbers. Given a number with no significant digits, but accuracy a, the Wolfram Language can then still tell that the actual value of the number must be in the range {-(10^(-a)),+(10^(-a))}/2. The Wolfram Language by default prints such numbers in the form 0.×(10^e).

Here is a number with 20-digit precision:

x=N[Exp[50],20]

Here there are no significant digits left:

Sin[x]/x

But the Wolfram Language still keeps track of the accuracy of the result:

Accuracy[%]

Adding the result to an exact 1 gives a number with quite high precision:

1+%%

One subtlety in characterizing numbers by their precision is that any number that is consistent with zero must be treated as having zero precision. The reason for this is that such a number has no digits that can be recognized as significant, since all its known digits are just zero.

This gives a number whose value is consistent with zero:

d=N[Pi,20]-Pi

The number has no recognizable significant digits of precision:

Precision[d]

But it still has a definite accuracy that characterizes the uncertainty in it:

Accuracy[d]

If you do computations whose results are likely to be near zero, it can be convenient to specify the accuracy, rather than the precision, that you want to get.

Specifying accuracy as well as precision.

Here is a symbolic expression:

u=ArcTan[1/3]-ArcCot[3]

This shows that the expression is equivalent to zero:

FullSimplify[u]

(RefLink[N, paclet:ref/N]) cannot guarantee to get a result to precision 20:

N[u,20]

But it can get a result to accuracy 20:

N[u,{Infinity,20}]

When the Wolfram Language works out the potential effect of unknown digits in arbitrary-precision numbers, it assumes by default that these digits are completely independent in different numbers. While this assumption will never yield too high a precision in a result, it may lead to unnecessary loss of precision.

In particular, if two numbers are generated in the same way in a computation, some of their unknown digits may be equal. Then, when these numbers are, for example, subtracted, the unknown digits may cancel. By assuming that the unknown digits are always independent, however, the Wolfram Language will miss such cancellations.

Here is a number computed to 20-digit precision:

d=N[3^-30,20]

The quantity 1+d has about 34-digit precision:

Precision[1+d]

This quantity has lower precision, since the Wolfram Language assumes that the unknown digits in each number d are independent:

Precision[(1+d)-d]

Numerical algorithms sometimes rely on cancellations between unknown digits in different numbers yielding results of higher precision. If you can be sure that certain unknown digits will eventually cancel, then you can explicitly introduce fixed digits in place of the unknown ones. You can carry these fixed digits through your computation, then let them cancel, and get a result of higher precision.

Functions for modifying precision and accuracy.

This introduces 10 more digits in d:

d=SetPrecision[d,30]

The digits that were added cancel out here:

(1+d)-d

The precision of the result is now about 44 digits, rather than 34:

Precision[%]

(RefLink[SetPrecision, paclet:ref/SetPrecision]) works by adding digits which are zero in base 2. Sometimes, the Wolfram Language stores slightly more digits in an arbitrary-precision number than it displays, and in such cases, (RefLink[SetPrecision, paclet:ref/SetPrecision]) will use these extra digits before introducing zeros.

This creates a number with a precision of 40 decimal digits. The extra digits come from conversion to base 10:

SetPrecision[0.400000000000000,40]

Global precision control parameters.

By making the global assignment (RefLink[$MinPrecision, paclet:ref/$MinPrecision])=n, you can effectively apply (RefLink[SetPrecision, paclet:ref/SetPrecision])[expr,n] at every step in a computation. This means that even when the number of correct digits in an arbitrary-precision number drops below n, the number will always be padded to have n digits.

If you set (RefLink[$MaxPrecision, paclet:ref/$MaxPrecision])=n as well as (RefLink[$MinPrecision, paclet:ref/$MinPrecision])=n, then you can force all arbitrary-precision numbers to have a fixed precision of n digits. In effect, what this does is to make the Wolfram Language treat arbitrary-precision numbers in much the same way as it treats machine numbers—but with more digits of precision.

Fixed-precision computation can make some calculations more efficient, but without careful analysis you can never be sure how many digits are correct in the results you get.

Here is a small number with 20-digit precision:

k=N[Exp[-60],20]

With the Wolfram Language’s usual arithmetic, this works fine:

Evaluate[1+k]-1

This tells the Wolfram Language to use fixed-precision arithmetic:

$MinPrecision=$MaxPrecision=20

The first few digits are correct, but the rest are wrong:

Evaluate[1+k]-1