How to | Control the Precision and Accuracy of Numerical Results

*Mathematica* 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,

*Mathematica* tracks the propagation of the numerical error. The use of high-precision numbers can yield accurate results where other numerical systems fail.

*Mathematica* gives exact answers whenever possible:

Out[206]= | |

Use

N to get a numerical approximation to an exact number:

Out[207]= | |

In

*Mathematica*, "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:

Out[208]= | |

"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:

Out[91]= | |

Use

Precision and

Accuracy to find the precision and accuracy of any number. Confirm the precision and accuracy of the results from

N:

Out[29]= | |

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:

Out[42]= | |

*Mathematica* 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:

Out[225]= | |

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

MachinePrecision:

Out[106]= | |

By default, the result of

N is a machine-precision number:

Out[148]= | |

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

Out[172]= | |

Arbitrary-precision numbers are entered using

and

. Enter an arbitrary-precision number with 10-digit precision and a similar number with 10-digit accuracy:

Out[4]= | |

Out[5]= | |

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:

Out[6]= | |

Specifying that

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

Out[7]= | |

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

Out[184]= | |

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):

Out[211]//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:

Out[205]= | |

To be sure of

n correct digits, use

N. Get 10 correct digits:

Out[207]= | |

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:

Out[241]= | |

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:

Out[248]= | |

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:

Out[246]= | |

When an expression contains approximate numbers, you do not normally need to use

N.

*Mathematica* 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:

Out[286]= | |

Use

Precision on the most recent output (

) to see that

*Mathematica* was able to determine that the result has 11 digits of precision:

Out[287]= | |

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:

Out[28]= | |

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

Out[296]= | |

Out[297]= | |

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:

Out[5]= | |

Out[6]= | |

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:

Out[166]= | |

Out[167]= | |