How to | Control the Precision and Accuracy of Numerical Results

The Wolfram Language works with both exact quantities and approximate numbers. Using N, you can obtain a numerical approximation to an exact quantity with any desired precision or accuracy. In calculations involving arbitrary-precision approximate numbers, the Wolfram Language tracks the propagation of the numerical error. The use of high-precision numbers can yield accurate results where other numerical systems fail.

The Wolfram Language gives exact answers whenever possible:

In[1]:=
Click for copyable input
Out[1]=

Use N to get a numerical approximation to an exact number:

In[2]:=
Click for copyable input
Out[2]=

In the Wolfram Language, "precision" effectively refers to the number of significant figures. You can specify the precision of the result with the second argument of N. Get a result with precision 50:

In[3]:=
Click for copyable input
Out[3]=

"Accuracy" effectively refers to the number of correct digits after the decimal point. Give as the second argument of N to get a result with accuracy a. The means any precision. For example, specify an accuracy of 10:

In[4]:=
Click for copyable input
Out[4]=

Use Precision and Accuracy to find the precision and accuracy of any number. Confirm the precision and accuracy of the results from N:

In[5]:=
Click for copyable input
Out[5]=

Use to directly enter a number you know is correct to n digits of precision, and to directly enter a number you know is correct to a digits of accuracy:

In[6]:=
Click for copyable input
Out[6]=
    

The Wolfram Language works with exact numbers and with two different types of approximate numbers: machine-precision numbers that take advantage of specialized hardware for fast arithmetic on your computer, and arbitrary-precision numbers that are correct to a specified number of digits.

Exact numbers have infinite precision and accuracy:

In[1]:=
Click for copyable input
Out[1]=

Numbers entered in decimal notation are normally interpreted as machine-precision numbers, which have precision MachinePrecision:

In[2]:=
Click for copyable input
Out[2]=

By default, the result of N is a machine-precision number:

In[3]:=
Click for copyable input
Out[3]=

Machine-precision numbers normally contain slightly under 16 digits, corresponding to exactly 53 bits:

In[4]:=
Click for copyable input
Out[4]=

Arbitrary-precision numbers are entered using and . Enter an arbitrary-precision number with 10-digit precision and a similar number with 10-digit accuracy:

In[5]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=

Specifying that has precision 10 implies a certain value for its accuracy, which in this case is less than 10. The difference (Precision[x]-Accuracy[x]) is essentially the scale, or magnitude, of the number, which is given by RealExponent[x]:

In[7]:=
Click for copyable input
Out[7]=

Specifying that has accuracy 10 implies a certain value for its precision, in this case greater than 10:

In[8]:=
Click for copyable input
Out[8]=

The accuracy of a number can be negative, indicating that all the correctly known digits are to the left of the decimal point:

In[9]:=
Click for copyable input
Out[9]=
    

MachinePrecision is considered less precise than any other precision. This is because, while machine-precision numbers always store slightly under 16 digits, no specific number of those digits are known to be correct.

Here, all of the digits in the machine-precision result are correct (use InputForm to display all the nonzero digits of an approximate number):

In[1]:=
Click for copyable input
Out[1]//InputForm=

Here, none of the digits in the machine-precision result are correct, due to the subtraction of two numbers that are nearly identical when represented in machine precision:

In[2]:=
Click for copyable input
Out[2]=

To be sure of n correct digits, use N[expr,n]. Get 10 correct digits:

In[3]:=
Click for copyable input
Out[3]=

N cannot give results at higher precision than its input. Here, it is impossible for N to obtain a result with 100 digits of precision because the input is only precise to 10 digits:

In[4]:=
Click for copyable input
Out[4]=

Here, it is impossible for N to obtain a result with 6 digits of precision because the input is a machine-precision number, which is considered less than any other precision:

In[5]:=
Click for copyable input
Out[5]=

If you know that an approximate number num is more precise than Precision[num] indicates, you can explicitly set its precision using SetPrecision. Get an arbitrary-precision number with 20 digits of precision corresponding to the machine-precision number 0.5:

In[6]:=
Click for copyable input
Out[6]=
    

When an expression contains approximate numbers, you do not normally need to use N. The Wolfram Language automatically computes an approximate result, and determines the precision of the result by considering the precision of the input. In general, the precision of the result will differ from the precision of any part of the input.

Add a number with 10-digit precision to a number with 1000-digit precision:

In[1]:=
Click for copyable input
Out[1]=

Use Precision on the most recent output () to see that the Wolfram Language was able to determine that the result has 11 digits of precision:

In[2]:=
Click for copyable input
Out[2]=

Because machine-precision numbers are considered less precise than any other numbers, whenever the input contains machine-precision numbers, the result is normally a machine-precision number.

Use (the short form of Map) to apply Precision to each part of a list of calculations involving machine-precision numbers:

In[3]:=
Click for copyable input
Out[3]=

Often, the precision of the result is not very different from the lowest precision part of the input:

In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=

In some cases the precision of the result can be much smaller than the precision of the input. This most often occurs due to the subtraction of two approximate numbers that are very nearly equal:

In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=

When you require results to a specific precision or accuracy, it is usually best to give exact inputs and use N. This is because N will sometimes temporarily compute at a higher precision than you request in order to obtain a result that satisfies your specification.

Get an approximation to with 20 digits of precision:

In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=