Wolfram Research, Inc.
3.1.4 Numerical Precision
As discussed in Section 1.1.2, Mathematica can handle approximate real numbers with any number of digits. In general, the precision of an approximate real number is the number of decimal digits in it which are treated as significant for computations. The accuracy of an approximate real number is the number of these digits which appear to the right of the decimal point. Precision is thus a measure of the relative error in a number, while accuracy is a measure of absolute error.
Precision and accuracy of real numbers.
Here is an approximate real number.
In:= xacc = 431.123145333555141444
This gives the total number of digits entered to specify the real number.
This gives the number of digits which appear to the right of the decimal point.
When you use N[expr, n], Mathematica tries to evaluate the expression expr to n digits of precision. And so long as your input has sufficient precision, Mathematica will usually succeed in getting a result with the requested precision.
This asks for Pi^25 to be evaluated to 30 digits of precision.
In:= N[Pi^25, 30]
The result indeed has 30 digits of precision.
If you use N[expr], and do not explicitly specify the precision of numbers to use, Mathematica will use machine-precision numbers.
In general, Mathematica distinguishes two kinds of approximate real numbers: arbitrary-precision ones, and machine-precision ones. Arbitrary-precision numbers can contain any number of digits, and their precision is adjusted during computations. Machine-precision numbers, on the other hand, contain a fixed number of digits, and their precision remains unchanged throughout computations.
As discussed in more detail below, machine-precision numbers work by making direct use of the numerical capabilities of your underlying computer system. As a result, computations with them can be done quickly. However, machine-precision numbers are much less flexible than arbitrary-precision ones.
This evaluates Pi using machine precision.
On the computer system used to generate this example, the machine precision is 16 decimal digits.
This gives the machine precision on the computer system.
When you enter an approximate real number, Mathematica has to decide whether to treat it as machine precision or arbitrary precision. Unless you specify otherwise, then if you give less than $MachinePrecision digits, Mathematica 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 Mathematica 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 Mathematica, and then later use them as input to Mathematica, you need to make sure that no information gets lost.
In standard output form, Mathematica prints a number like this to six digits.
In input form, Mathematica prints all the digits it knows.
Here is an arbitrary-precision number in standard output form.
In:= N[Pi, 20]
In input form, Mathematica explicitly indicates the precision of the number, and gives extra digits to make sure the number can be reconstructed correctly.
This makes Mathematica not explicitly indicate precision.
In:= InputForm[%, NumberMarks->False]
Out//InputForm= InputForm[3.141592653589793238462643383279503`20, NumberMarks -> False]
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 Mathematica by default always include number marks in input form.
In:= $NumberMarks = True
Even a machine-precision number is now printed with an explicit number mark.
Even with no number marks, InputForm still uses *^ for scientific notation.
In:= InputForm[N[Exp, 20], NumberMarks->False]
Out//InputForm= InputForm[ 3.77302030092993982340143119348313509597`20*^260 , 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 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.
In:= Exp[ N[2 Pi I] ]
You can get rid of the imaginary part using Chop.