[R] : Ramanujan and the accuracy of floating point computations - using Rmpfr in R

Hadley Wickham h.wickham at gmail.com
Sat Jul 4 08:44:21 CEST 2015


>>>> It doesn’t appear to me that mpfr was ever designed to handle expressions as the first argument.
>>>
>>> This could be a start. Obviously one would wnat to include code to do other substitutions probably using the all.vars function to pull out the other “constants” and ’numeric’ values to make them of equivalent precision. I’m guessing you want to follow the parse-tree and then testing the numbers for integer-ness and then replacing by paste0( “mpfr(“, val, “L, “, prec,”)” )
>>>
>>> Pre <- function(expr, prec){ sexpr <- deparse(substitute(expr) )
>>
>> Why deparse?  That's almost never a good idea.  I can't try your code (I
>> don't have mpfr available), but it would be much better to modify the
>> expression than the text representation of it.  For example, I think
>> your code would modify strings containing "pi", or variables with those
>> letters in them, etc.  If you used substitute(expr) without the
>> deparse(), you could replace the symbol "pi" with the call to the Const
>> function, and be more robust.
>>
>
> Really? I did try. I was  fairly sure that someone could do better but I don’t see an open path along the lines you suggest. I’m pretty sure I tried `substitute(expr, list(pi= pi))` when `expr` had been the formal argument and got disappointed because there is no `pi` in the expression `expr`. I _thought_ the problem was that `substitute` does not evaluate its first argument, but I do admit to be pretty much of a klutz with this sort of programming. I don’t think you need to have mpfr installed in order to demonstrate this.

You might want to read http://adv-r.had.co.nz/Expressions.html - it's
my best attempt at explaining how to modify call trees in R.

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list