[R] Extending the accuracy of exp(1) in R

(Ted Harding) ted.harding at wlandres.net
Tue Nov 9 14:42:20 CET 2010


On 09-Nov-10 13:21:10, Shant Ch wrote:
> Hi,
> I want to use a more accurate value of exp(1)._ The value given
> by R is 2.718282. I want a value which has more than 15 decimal
> places. Can anyone let me know how can I increase the accuracy level.
> 
> Actually there are some large multipliers of exp(1) in my whole
> expression, and I want a more accurate result at the last step
> of my program, and for that I need to use highly accurate value
> of exp(1).
> 
> Can anyone help me out? Thanks.
> Shant

Where the value of exp(1) as computed by R is concerned, you have
been deceived by what R displays ("prints") on screen. The default
is to display any number to 7 digits of accuracy, but that is not
the accuracy of the number held internally by R:

  exp(1)
  # [1] 2.718282
  exp(1) - 2.718282
  # [1] -1.715410e-07

You can get a particular result to be displayed to greater accuracy
by using print(..., digits=...) (and the "digits" argument is the
one in second position, so you don;t necessarily need to use "digits=".
Therefore:

  print(exp(1),17)
  # [1] 2.718281828459045

(and you will not get greater precision by using more digits,
e.g. 18,19,...).

  print(exp(1)-2.718281828459045,19)
  [1] 0

So 2.718281828459045 is the greatest accuracy you can get with
normal 32-bit R. This is the value which will be computed by R
for any instance of exp(1), and which will be stored as the value
of Y in:

  Y <- exp(1)

If you want to see all computed results displayed to more than
7 digits of accuracy, you can set the global "digits" option:

  options(digits=17)
  exp(1)
  # [1] 2.718281828459045
  pi
  # [1] 3.141592653589793

See the entry for "digits" in ?options.

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 09-Nov-10                                       Time: 13:42:17
------------------------------ XFMail ------------------------------



More information about the R-help mailing list