An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100108/3e78002f/attachment.pl>
Newbie question on precision
5 messages · Paul Evans, (Ted Harding), jim holtman +1 more
On 08-Jan-10 16:56:25, Paul Evans wrote:
Hi all, How can I get R to change the default precision value? For example:
x=0.99999999999999999 1-x
[1] 0 Is there a way that I can get a non-zero value using some parameter, or some package? many thanks.
The problem here is that, as far as R in concerned, once you have entered x=0.99999999999999999 then x is *exactly* 1. Namely, your x is 1 - 1e-17, and the two things you need to take note of are: .Machine$double.eps # [1] 2.220446e-16 .Machine$double.neg.eps # [1] 1.110223e-16 and the descriptions in ?.Machine which say: "double.eps: the smallest positive floating-point number 'x' such that '1 + x != 1'." "double.neg.eps: a small positive floating-point number 'x' such that '1 - x != 1'. " Both of these (in particular "double.neg.eps") are greater than 1e-17, so your x was stored as 0. I believe there may be packages which can work to greater precision, but I have to leave it to others to describe them (if any). Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 08-Jan-10 Time: 17:39:47 ------------------------------ XFMail ------------------------------
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100108/49f8867f/attachment.pl>
Paul Evans wrote:
How can I get R to change the default precision value? For example:
x=0.99999999999999999 1-x
[1] 0 Is there a way that I can get a non-zero value using some parameter, or some package? many thanks.
The 'gmp' package allows calculation with arbitrary precision rationals (and every finite-digit decimal is a rational). See a recent post of mine, listing some gmp examples: http://tolstoy.newcastle.edu.au/R/e9/help/10/01/0579.html
On 1/8/2010 1:29 PM, Magnus Torfason wrote:
Paul Evans wrote:
How can I get R to change the default precision value? For example:
x=0.99999999999999999 1-x
[1] 0 Is there a way that I can get a non-zero value using some parameter, or some package? many thanks.
The 'gmp' package allows calculation with arbitrary precision rationals (and every finite-digit decimal is a rational). See a recent post of mine, listing some gmp examples: http://tolstoy.newcastle.edu.au/R/e9/help/10/01/0579.html
And some more looking uncovered the 'Rmpfr' package, which allows
arbitrary precision floating point calculations. Example:
> x = mpfr("0.99999999999999999", precBits=100)
> x
1 'mpfr' number of precision 100 bits
[1] 0.99999999999999998999999999999944
> 1-x
1 'mpfr' number of precision 100 bits
[1] 1.0000000000000556923992179579934e-17
It does not store decimals exactly (as a rational would), because the
exponent is base 2 rather than base 10. However, it has parsing and
as.string functions that return decimal-formatted strings (whereas the
bigq class in gmp would require such functions to be written manually).
I guess for people who are looking for arbitrary precision decimal
fractions the the choice of these packages becomes a choice of style or
specific application needs.
Best,
Magnus