[R] Newbie question on precision
Magnus Torfason
zulutime.net at gmail.com
Fri Jan 8 20:00:53 CET 2010
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
More information about the R-help
mailing list