7

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.

Oliver Spryn
  • 359
  • 4
  • 12

4 Answers4

12

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
Brett Champion
  • 20,779
  • 2
  • 64
  • 121
  • 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. – Dr_Zaszuś May 31 '18 at 11:51
11

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
Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
10
NIntegrate[f[x], {x, 0, 2*Pi}, WorkingPrecision -> 15, PrecisionGoal -> 10]
 7.95492652101285
Artes
  • 57,212
  • 12
  • 157
  • 245
2

Use in the following way also.

 SetPrecision[NIntegrate[f[x], {x, 0, 2*Pi}], 15]
subbu
  • 2,304
  • 1
  • 13
  • 32