7
$\begingroup$

How can I set the output precision of the following statement to 10 decimal places? I was looking through the documentation, and for some reason, all I could find was Accuracy[] or Precision[].

f[x_] := E^Cos[x]
NIntegrate[f[x], {x, 0, 2*Pi}]

Which, by default, evaluates to:

7.95493

Thank you for your time.

$\endgroup$
  • 2
    $\begingroup$ This is all in the documentation: N, guides to numerical precision, presentation of numbers using NumberForm. Please at least make some effort with the documentation. $\endgroup$ – Verbeia Mar 28 '12 at 3:56
  • $\begingroup$ @Verbeia I was looking through the documentation, and for some reason, all I could find was Accuracy[] or Precision[] but nothing like what Artes mentioned. $\endgroup$ – Oliver Spryn Mar 28 '12 at 3:58
  • 2
    $\begingroup$ It is helpful to mention this in your question, so we know what you've tried. Anyway, the documentation for Precision links to the tutorial I mentioned and to the guide page that mentions PrecisionGoal and AccuracyGoal. It was all one click away. $\endgroup$ – Verbeia Mar 28 '12 at 4:04
  • $\begingroup$ Thank you Verbeia. I will keep this in mind! $\endgroup$ – Oliver Spryn Mar 28 '12 at 4:06
  • 2
    $\begingroup$ It's notable that the documentation for Precision links only indirectly through the tutorials to NumberForm. The direct links are only Accuracy, RealExponent, N, Chop, SetPrecision, MachineNumberQ, MachinePrecision, PrecisionGoal, WorkingPrecision, ExactNumberQ, NumberMarks. IMO, it'a a lot to go trawling through when looking for this answer. $\endgroup$ – Reb.Cabin Mar 31 '12 at 0:07
8
$\begingroup$
NIntegrate[f[x], {x, 0, 2*Pi}, WorkingPrecision -> 15, PrecisionGoal -> 10]
 7.95492652101285
$\endgroup$
  • $\begingroup$ Oh yeah! Just what I needed. Thank you, Artes! $\endgroup$ – Oliver Spryn Mar 28 '12 at 3:56
11
$\begingroup$

The number of digits displayed for machine reals can also be controlled via a setting in the preferences dialog. Under Appearance > Formatting > Numbers, change the value for Number of digits displayed in output:

In[4]:= f[x_] := E^Cos[x]
In[5]:= NIntegrate[f[x], {x, 0, 2*Pi}]

Out[5]= 7.954926521

Note that this doesn't affect arbitrary-precision numbers, which show the digits appropriate for their precisions (shown in these examples after the backticks):

In[6]:= 1.23456789`4

Out[6]= 1.235

In[7]:= 1.23456789`14

Out[7]= 1.2345678900000
$\endgroup$
  • $\begingroup$ This has a downside of cluttering your notebook. The solution of @Vitaliy Kaurov is better in this respect. It only produces the long form numbers, when required. $\endgroup$ – Dr_Zaszuś May 31 '18 at 11:51
9
$\begingroup$

If you just want "10 decimal places" - you already have them and more. Mathematica just displays shorter version. You get your answer on default with 16 significant digits, - use NumberForm to see it:

f[x_] := E^Cos[x]
NumberForm[NIntegrate[f[x], {x, 0, 2*Pi}], 100]
 7.954926521012846

I intentionally exaggerated with 100 digit precision so you can see where the default digits are cut off. Also if SHIFT+ENTER (evaluate) your answer/number (output cell), you will see it again - your 16 digits appear in the input and shorten in the output:

In[1]:= 7.954926521012846`

Out[1]= 7.95493
$\endgroup$
2
$\begingroup$

Use in the following way also.

 SetPrecision[NIntegrate[f[x], {x, 0, 2*Pi}], 15]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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