9
$\begingroup$

I've been reading the documentation in Mathematica about precision, namely:

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

$\endgroup$
1
  • $\begingroup$ I have a follow-up question here that was too long for a comment. $\endgroup$
    – rhermans
    Commented Sep 19, 2017 at 17:43

1 Answer 1

15
$\begingroup$

Here are my thoughts:

Q1

Machine numbers: For machine numbers, what you describe is correct. I would just add that you can use InputForm or FullForm to see all the digits if desired:

N[Pi]
% //InputForm

3.14159

3.141592653589793

Extended precision numbers: For extended precision numbers (i.e., numbers whose precision is a number, and not MachinePrecision), Mathematica always displays all correct digits. PrintPrecision plays no role for these numbers.

Q2

If you have any machine numbers in the input, then the computations will be done by coercing all numbers into machine numbers. Machine numbers have no precision tracking, so there is no guarantee that any of the digits are correct.

On the other hand, if the input only contains exact numbers and extended precision numbers, then the computations will performed with precision tracking, and the output will in general be an extended precision number, and the digits that are output should be correct.

Q3

For machine numbers, the precision (approximately 16) does not indicate anything about the number of correct digits. For extended precision numbers, the precision does indicate how many digits are correct.

Summary

If the output is a machine number, then the number of correct digits is not known.

If the output is an extended precision number, then (barring bugs) the number of correct digits is equal to the precision of the output.

$\endgroup$
4
  • $\begingroup$ Thank you, @CarlWoll. Then, my question is: How can I force Mathematica to treat all my input numbers and numbers resulting from intermediate calculations as extended precision numbers? $\endgroup$
    – Vicent
    Commented Sep 19, 2017 at 14:47
  • 6
    $\begingroup$ Use extended precision numbers as input, e.g., 1.1`20 instead of 1.1. $\endgroup$
    – Carl Woll
    Commented Sep 19, 2017 at 14:49
  • $\begingroup$ @Carl Woll, Is there a general schemes/option to force this extended precision numbers in all input numbers and numbers resulting? $\endgroup$
    – Betatron
    Commented May 21, 2019 at 18:07
  • $\begingroup$ @Betatron, yes, there is. Use @MinPrecision to modify the global minimum precision applied everywhere in your script. $\endgroup$ Commented May 11, 2021 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.