I've been reading the documentation in Mathematica about precision, namely:
- Exact and Approximate Results
- Arbitrary-Precision Numbers
- Arbitrary-Precision Calculations
- Machine-Precision Numbers
- Numerical Precision
Also, (the answers and comments to) these questions helped me a lot to understand things:
However, I would like to ask some questions to be sure of having understood how to manage with precision in Mathematica in practice.
(Q1) Actual vs printed precision. I did not understand many things about precision in Mathematica until I understood this. The (inner or actual) precision of a given number (i.e., how many significant digits it has, in storage) can be different to (greater than, generally) the number of digits that are shown when this number is printed out. The former has to do with actual precision (that can be shown using Precision
or can be controlled at calculation time by functions such as N
, etc.), while the latter is related with functions such as NumberForm
or notebook options such as PrintPrecision
. Is this right?? Any (subtle) details regarding actual vs printed precision that is worthy to know about?
For instance, I can ask Mathematica to show the value of $\pi$:
N[Pi] (* Is there any other way to force it to be shown as a number? *)
and I'll get this result:
3.14159
, which does not mean that $\pi$ is stored only with 6 digits of precision, of course, but that only its first 6 digits are shown (by default).
Actually,
Precision[Pi]
returns
\[Infinity]
(as it is still in its symbolic form) while
N[Pi]
Precision[%]
returns
MachinePrecision
(which happens to be $MachinePrecision
$\approx$ 16, in my case), and not 6.
So, as far as I understand, if I use N[Pi]
as a part of further calculations, it will be treated as a number with 16 digits of precision (i.e., significant or correct digits), and not only as 3.15159. Is this right?
(Q2) How to ensure a reasonable precision in the result returned by a built-in function. Following on from this, what must I do in order to ensure enough precision in a built-in function that performs some inner partial calculations before reaching to the final result that is returned by the function? I mean, something like this:
myfunction[a_,b_] :=
(* Just a silly function *)
Module[
(* Some local variables *)
{x, y, z}
,
(* The function starts here *)
x = a + b ;
y = x^2 ;
z = a - b*y*x ;
(* Returned value *)
x/z (* No final `;` here! *)
]
If the arguments of the function a
and b
are given as numbers (not symbols), can I be sure that the result of the function is stored with a reasonable precision (say, machine precision, for instance)? Or should I add any Mathematica sentence to ensure that no partial result (the output of calculating x
, y
or z
in my example) is calculated with less precision than possible and, thus, the final result is stored with less precision than expected? This is actually my only concern about precision when working with Mathematica.
(Q3) Just to be sure: The precision of any number n
in Mathematica is equal to the number of digits of n
that you can rely on? I mean, if any partial result of my built-in function has a precision of, say, 16, does it mean that the number of correct digits of this number, as calculated and stored in Mathematica, is 16 (i.e., this number is correctly calculated up to its 16th digit)?