# Setting the Accuracy of calculations

I need to optimize an expression that involves a number of trigonometric functions and Exp[]. How do I make sure that all my calculations have an accuracy of 120-200 digits after the decimal point? This includes the accuracy of Exp[] and trig functions.

To get my point across, here is part of the equation:

z[x_, y_]:= Exp[Sin[60.0*x]] + Sin[50.0*Exp[y]]


Mathematica lets you control Precision of computations (which is total number of digits in the number) with two global variables: $MinPrecision and $MaxPrecision. However, I am not looking for precision.

• Do you know that keeping expressions in terms of integers and rationals will keep exact values? Try NestList[16 # (1 - #)/3 &, 1/5, 4] ? Though for heavy computations you will lose speed. – Vitaliy Kaurov Jul 1 '12 at 17:53
• I am aware that Mathematica will give me exact values for integer & rational calculations, but my calculations are far from exact. – newprint Jul 1 '12 at 18:00
• It seems that N[expr, {Infinity, accuracy}] might be the way to go, assuming the inputs are known to sufficient precision. (If the inputs are aren't, then you cannot know the result to the desired accuracy.) – Michael E2 Aug 6 '15 at 21:37

z[x_, y_] := Exp[Sin[60*x]] + Sin[50*Exp[y]]
z[SetAccuracy[20., 200], SetAccuracy[20., 200]] // Accuracy


does this not do what you need?

• In:= z[SetAccuracy[20., 200], SetAccuracy[20., 200]] // Accuracy Out= 13.6508 Don't think it worked. – newprint Jul 1 '12 at 18:25
• @newprint Sorry, I forgot to add the modified definition of z I used. Or, is it that you simply don't know the coeffs? Because that's different... – acl Jul 1 '12 at 18:47
• Thanks, I understood the SetAccuracy and // Accuracy – newprint Jul 2 '12 at 1:13

If you use inexact constant in your equation it helps if you increase their accuracy as well. You can do that easily using the backtick notation:

z[x_, y_] := Exp[Sin[60.0200*x]] + Sin[50.0200*Exp[y]]
z[SetAccuracy[20., 200], SetAccuracy[20., 200]] // Accuracy


190.0318717

• Initially, I used the backtick for floating point values as well(I read about in Mathematical Cookbook), though it becomes somewhat cumbersome within long equation. – newprint Jul 2 '12 at 1:19

If you want Mathematica to return arbitrary-precision calculations with (approximately) a given Accuracy you could ask for it in MantissaExponent form:

MantissaExponent[Exp 12350][] // Accuracy

52.8558


### Supporting information

Accuracy of input numbers can be set with double backticks:

12345.567850   // Accuracy
1234550        // Accuracy

50.
50.


Single backticks define Precision:

12345.567850   // Precision
1234550        // Precision

50.
50.


A "constant accuracy" doesn't make much sense for floating point calculations. Mathematica tracks and preserves Precision. This is called "arbitrary-precision" after all.

Exp 12350  // Precision
Exp 12350 // Accuracy

50.
-15.1442

• I think in the "Short answer" section you use wrong formulation because MantissaExponent does not alter the accuracy of the result (I know, you know it - just for correctness). The right formulation could be: "If you want Mathematica to give you the accuracy of the result of calculations you could ask for it using MantissaExponent form: ..." – Alexey Popkov Jul 2 '12 at 17:43
• @Alexey I wasn't sure how to say what I wanted to. I'll try to rephrase it as you suggest later today. – Mr.Wizard Jul 2 '12 at 18:00
• One problem is that Wolfram's understanding of precision and accuracy is significantly misleading and the second one is that you use in this sentence the term "accuracy" with another meaning: the number of precise digits right to the decimal point in ScientificForm. In other words, it is something always approximately equal to Precision minus 1. – Alexey Popkov Jul 2 '12 at 18:14
• Discussion on Precision and Accuracy` in Mathematica: groups.google.com/d/msg/comp.soft-sys.math.mathematica/… – Alexey Popkov Jul 2 '12 at 18:20