Setting precision globally

This question is similar to this one and to this one. My code involves a lot of calls to Mathematica's N function, with the second argument (precision) set to 100 (say). I'm happy with the code, it does what it's supposed to do. All I am a bit uneasy about is the need to explicitly specify the desired precision in each and every call to the N function. I made the precision a global variable, so I can easily change it, but the code would be easier to read if the second argument of the N function was somehow set to be by default equal to the desired accuracy. Is that doable at all? I am new to Mathematica. Thanks much in advance.

• Since it is only for N that you wish to use a specific precision, you could define a replacement function, e.g., nNN[x_] := N[x, myprecision]. Oct 9 '15 at 1:48
• @bbgodfrey: Thanks, that would do it. I thought though that Mathematica had a "built-in" way to achieve the same effect.
– Alex
Oct 9 '15 at 2:01
• Well setting $MinPrecision is this built-in way, but you have to be careful to not use machine precision in the input, i.e. don't use 1., but 1. Oct 9 '15 at 6:57 • @Jansen But this$MinPrecision = 20; Precision@N still gives me MachinePrecision. Oct 9 '15 at 10:01
• @Jansen This is a good point. I just discovered that changing one of the input parameters of the script from 1 to 1. produces a torrent of errors. However, if I write 1.'300 then the errors go away. Is there a way to tell Mathematica that by default all floating point constants are to be treated as having 300 decimal places? Setting $MinPrecision=300 doesn't help. Thanks. – Alex Oct 9 '15 at 11:57 1 Answer Numerics seem to always have gotchas lurking in the bushes, so one needs to be very careful about making global changes to the way numerics are done. To satisfy your requirements, I would probably put the following code in a initialization cell.$myPrecision = 25;
myN[x_] := N[x, $myPrecision] myP[x_] := SetPrecision[x,$myPrecision]

Of course, you would replace 25 with whatever precision you need for your calculations.

Providing myP is just as important as providing myN. All the explicit parameters and constants used in your computations must be passed through myP or you could very easily undo the work you are trying accomplish with myN.

In fact, one might argue that myP is more important than myN. For instance

{u, v, w} = myP /@ {1.5, 2., 30. °};
Precision[u + v Sin[w]]
24.7631

But with conventional parameter definitions, even though you apply myN you get

a = 1.5; b = 2.; c = 30. °;
Precision[myN[a + b Sin[c]]]
MachinePrecision

You may wonder why you can't write your parameters as

a = 1.5$myPrecision since a = 1.525 would work. Unfortunately, the first form is treated as multiplication of 1.5` by$myPrecision. This not only produces a machine precision real, it also produces the wrong value.