Numerical Precision

As discussed in "Exact and Approximate Results", the Wolfram Language can handle approximate real numbers with any number of digits. In general, the precision of an approximate real number is the effective number of decimal digits in it which are treated as significant for computations. The accuracy is the effective number of these digits which appear to the right of the decimal point. Note that to achieve full consistency in the treatment of numbers, precision and accuracy often have values that do not correspond to integer numbers of digits.

Precision and accuracy of real numbers.

This generates a number with 30-digit precision:

x=N[Pi^10,30]

This gives the precision of the number:

Precision[x]

The accuracy is lower since only some of the digits are to the right of the decimal point:

Accuracy[x]

This number has all its digits to the right of the decimal point:

x/10^6

Now the accuracy is larger than the precision:

{Precision[%],Accuracy[%]}

An approximate real number always has some uncertainty in its value, associated with digits beyond those known. One can think of precision as providing a measure of the relative size of this uncertainty. Accuracy gives a measure of the absolute size of the uncertainty.

The Wolfram Language is set up so that if a number x has uncertainty δ, then its true value can lie anywhere in an interval of size δ from x-δ/2 to x+δ/2. An approximate number with accuracy a is defined to have uncertainty (10^(-a)), while a nonzero approximate number with precision p is defined to have uncertainty |x|(10^(-p)).

Definitions of precision and accuracy in terms of uncertainty.

Adding or subtracting a quantity smaller than the uncertainty has no visible effect:

{x-10^-26,x,x+10^-26}

Numerical evaluation with arbitrary-precision and machine-precision numbers.

The Wolfram Language distinguishes two kinds of approximate real numbers: arbitrary-precision numbers, and machine-precision numbers or machine numbers. Arbitrary-precision numbers can contain any number of digits, and maintain information on their precision. Machine numbers, on the other hand, always contain the same number of digits, and maintain no information on their precision.

Here is a machine-number approximation to π:

N[Pi]

These are both arbitrary-precision numbers:

{N[Pi,4],N[Pi,20]}

As discussed in more detail below, machine numbers work by making direct use of the numerical capabilities of your underlying computer system. As a result, computations with them can often be done more quickly. They are however much less flexible than arbitrary-precision numbers, and difficult numerical analysis can be needed to determine whether results obtained with them are correct.

Machine numbers.

This returns the symbol (RefLink[MachinePrecision, paclet:ref/MachinePrecision]) to indicate a machine number:

Precision[N[Pi]]

On this computer, machine numbers have slightly less than 16 decimal digits:

$MachinePrecision

When you enter an approximate real number, the Wolfram Language has to decide whether to treat it as a machine number or an arbitrary-precision number. Unless you specify otherwise, if you give less than (RefLink[$MachinePrecision, paclet:ref/$MachinePrecision]) digits, the Wolfram Language will treat the number as machine precision, and if you give more digits, it will treat the number as arbitrary precision.

Input forms for numbers.

When the Wolfram Language prints out numbers, it usually tries to give them in a form that will be as easy as possible to read. But if you want to take numbers that are printed out by the Wolfram Language, and then later use them as input to the Wolfram Language, you need to make sure that no information gets lost.

In standard output form, the Wolfram Language prints a number like this to six digits:

N[Pi]

In input form, the Wolfram Language prints all the digits it knows:

InputForm[%]

Here is an arbitrary-precision number in standard output form:

N[Pi,20]

In input form, the Wolfram Language explicitly indicates the precision of the number, and gives extra digits to make sure the number can be reconstructed correctly:

InputForm[%]

This makes the Wolfram Language not explicitly indicate precision:

InputForm[%,NumberMarks->False]

Controlling printing of numbers.

The default setting for the (RefLink[NumberMarks, paclet:ref/NumberMarks]) option, both in (RefLink[InputForm, paclet:ref/InputForm]) and in functions such as (RefLink[ToString, paclet:ref/ToString]) and (RefLink[OpenWrite, paclet:ref/OpenWrite]) is given by the value of (RefLink[$NumberMarks, paclet:ref/$NumberMarks]). By resetting (RefLink[$NumberMarks, paclet:ref/$NumberMarks]), therefore, you can globally change the way that numbers are printed in (RefLink[InputForm, paclet:ref/InputForm]).

This makes the Wolfram Language by default always include number marks in input form:

$NumberMarks=True

Even a machine-precision number is now printed with an explicit number mark:

InputForm[N[Pi]]

Even with no number marks, (RefLink[InputForm, paclet:ref/InputForm]) still uses *^ for scientific notation:

InputForm[N[Exp[600],20],NumberMarks->False]

In doing numerical computations, it is inevitable that you will sometimes end up with results that are less precise than you want. Particularly when you get numerical results that are very close to zero, you may well want to assume that the results should be exactly zero. The function (RefLink[Chop, paclet:ref/Chop]) allows you to replace approximate real numbers that are close to zero by the exact integer 0.

Removing numbers close to zero.

This computation gives a small imaginary part:

Exp[N[2 Pi I]]

You can get rid of the imaginary part using (RefLink[Chop, paclet:ref/Chop]):

Chop[%]