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:
Use N to get a numerical approximation to an exact number:
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:
"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:
Use Precision and Accuracy to find the precision and accuracy of any number. Confirm the precision and accuracy of the results from N:
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:
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:
Numbers entered in decimal notation are normally interpreted as machine-precision numbers, which have precision MachinePrecision:
By default, the result of N is a machine-precision number:
Machine-precision numbers normally contain slightly under 16 digits, corresponding to exactly 53 bits:
Arbitrary-precision numbers are entered using and . Enter an arbitrary-precision number with 10-digit precision and a similar number with 10-digit accuracy:
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]:
Specifying that has accuracy 10 implies a certain value for its precision, in this case greater than 10:
The accuracy of a number can be negative, indicating that all the correctly known digits are to the left of the decimal point:
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):
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:
To be sure of n correct digits, use N[expr, n]. Get 10 correct digits:
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:
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:
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:
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:
Use Precision on the most recent output () to see that Mathematica was able to determine that the result has 11 digits of precision:
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:
Often, the precision of the result is not very different from the lowest precision part of the input:
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:
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: