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. [[alternative HTML version deleted]]
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 ------------------------------
FAQ 7.31 The precision of a floating point number is about 16 digits and your 'x' is at that limit. If you reduce it, you will see a result:> x=0.99999999999999999 > 1-x[1] 0> print(1-x, digits=20)[1] 0> x=0.99999999999999 > print(1-x, digits=20)[1] 0.00000000000000999200722162641>On Fri, Jan 8, 2010 at 11:56 AM, Paul Evans <p.evans48@yahoo.com> 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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
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