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[x] the total number of significant decimal digits in x Accuracy[x] the number of significant decimal digits to the right of the decimal point in x

Precision and accuracy of real numbers.

This generates a number with 30digit precision.
 In[1]:=
 Out[1]=
This gives the precision of the number.
 In[2]:=
 Out[2]=
The accuracy is lower since only some of the digits are to the right of the decimal point.
 In[3]:=
 Out[3]=
This number has all its digits to the right of the decimal point.
 In[4]:=
 Out[4]=
Now the accuracy is larger than the precision.
 In[5]:=
 Out[5]=

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 has uncertainty , then its true value can lie anywhere in an interval of size from to . An approximate number with accuracy is defined to have uncertainty , while a nonzero approximate number with precision is defined to have uncertainty .

 Precision[x] Accuracy[x]

Definitions of precision and accuracy in terms of uncertainty.

Adding or subtracting a quantity smaller than the uncertainty has no visible effect.
 In[6]:=
 Out[6]=
 N[expr,n] evaluate expr to n‐digit precision using arbitrary‐precision numbers N[expr] evaluate expr numerically using machine‐precision numbers

Numerical evaluation with arbitraryprecision and machineprecision numbers.

The Wolfram Language distinguishes two kinds of approximate real numbers: arbitraryprecision numbers, and machineprecision numbers or machine numbers. Arbitraryprecision 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 machinenumber approximation to .
 In[7]:=
 Out[7]=
These are both arbitraryprecision numbers.
 In[8]:=
 Out[8]=

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 arbitraryprecision numbers, and difficult numerical analysis can be needed to determine whether results obtained with them are correct.

 MachinePrecision the precision specification used to indicate machine numbers \$MachinePrecision the effective precision for machine numbers on your computer system MachineNumberQ[x] test whether x is a machine number

Machine numbers.

This returns the symbol MachinePrecision to indicate a machine number.
 In[9]:=
 Out[9]=
On this computer, machine numbers have slightly less than 16 decimal digits.
 In[10]:=
 Out[10]=

When you enter an approximate real number, the Wolfram Language has to decide whether to treat it as a machine number or an arbitraryprecision number. Unless you specify otherwise, if you give less than \$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.

 123.4 a machine‐precision number 123.45678901234567890 an arbitrary‐precision number on some computer systems 123.45678901234567890` a machine‐precision number on all computer systems 123.456`200 an arbitrary‐precision number with 200 digits of precision 123.456``200 an arbitrary‐precision number with 200 digits of accuracy 1.234*^6 a machine‐precision number in scientific notation () 1.234`200*^6 a number in scientific notation with 200 digits of precision 2^^101.111`200 a number in base 2 with 200 binary digits of precision 2^^101.111`200*^6 a number in base 2 scientific notation ()

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.
 In[11]:=
 Out[11]=
In input form, the Wolfram Language prints all the digits it knows.
 In[12]:=
 Out[12]//InputForm=
Here is an arbitraryprecision number in standard output form.
 In[13]:=
 Out[13]=
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.
 In[14]:=
 Out[14]//InputForm=
This makes the Wolfram Language not explicitly indicate precision.
 In[15]:=
 Out[15]//InputForm=
 InputForm[expr,NumberMarks->True] use ` marks in all approximate numbers InputForm[expr,NumberMarks->Automatic] use ` only in arbitrary‐precision numbers InputForm[expr,NumberMarks->False] never use ` marks

Controlling printing of numbers.

The default setting for the NumberMarks option, both in InputForm and in functions such as ToString and OpenWrite is given by the value of \$NumberMarks. By resetting \$NumberMarks, therefore, you can globally change the way that numbers are printed in InputForm.

This makes the Wolfram Language by default always include number marks in input form.
 In[16]:=
 Out[16]=
Even a machineprecision number is now printed with an explicit number mark.
 In[17]:=
 Out[17]//InputForm=
Even with no number marks, InputForm still uses *^ for scientific notation.
 In[18]:=
 Out[18]//InputForm=

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 Chop allows you to replace approximate real numbers that are close to zero by the exact integer 0.

 Chop[expr] replace all approximate real numbers in expr with magnitude less than by 0 Chop[expr,dx] replace numbers with magnitude less than dx by 0

Removing numbers close to zero.

This computation gives a small imaginary part.
 In[19]:=
 Out[19]=
You can get rid of the imaginary part using Chop.
 In[20]:=
 Out[20]=