On 2024-07-19 12:37 p.m., Martin Maechler wrote:
Khue Tran
     on Fri, 19 Jul 2024 09:32:14 +1000 writes:

     > Thank you for the suggestion, Denes, Vladimir, and Dirk. I have indeed
     > looked into Rmpfr and while the package can interface GNU MPFR with R
     > smoothly, as of right now, it doesn't have all the functions I need (ie.
     > eigen for mpfr class) and when one input decimals, say 0.1 to mpfr(), the
     > precision is still limited by R's default double precision.

Well, it depends *how* you input 0.1:
the double precision representation of 0.1 is *NOT* the same as
mathematically 1/10 and it cannot be.

What you really want is either

mpfr(0.1, 100) # 100 as an example ... to get a more precise
               # version of the '0.1' (double prec).

## *or*, e.g.,

1 / mpfr(10, 100) # which is a more accurate approximation to
                   # the mathematical fraction 1/10

It seems like mpfr("0.1", 100) works pretty well. I'd assume it does the parsing, rather than parse 0.1 to the closest double, and convert that.

Duncan Murdoch



## if you really want that, I'd also recommend truly exact fractions

require(gmp)
as.bigq(1, 10) # or equivalently

1 / as.bigz(10) # which automatically makes an exact fraction

     > Thank you for the note, Dirk. I will keep in mind to send any future
     > questions regarding Rcpp to the Rcpp-devel mailing list. I understand 
that
     > the type used in the Boost library for precision is not one of the types
     > supported by SEXP, so it will be more complicated to map between the cpp
     > codes and R. Given Rmpfr doesn't provide all necessary mpfr calculations
     > (and embarking on interfacing Eigen with Rmpfr is not a small task), does
     > taking input as strings seem like the best option for me to get precise
     > inputs?

almost surely (that's also what you can do in Rmpfr)..

Martin


     > Sincerely,
     > Khue

     > On Fri, Jul 19, 2024 at 8:29 AM Dirk Eddelbuettel <e...@debian.org> 
wrote:

     >>
     >> Hi Khue,
     >>
     >> On 19 July 2024 at 06:29, Khue Tran wrote:
     >> | I am currently trying to get precise inputs by taking strings instead 
of
     >> | numbers then writing a function to decompose the string into a 
rational
     >> | with the denominator in the form of 10^(-n) where n is the number of
     >> | decimal places. I am not sure if this is the only way or if there is a
     >> | better method out there that I do not know of, so if you can think of 
a
     >> | general way to get precise inputs from users, it will be greatly
     >> | appreciated!
     >>
     >> That is one possible way. The constraint really is that the .Call()
     >> interface
     >> we use for all [1] extensions to R only knowns SEXP types which map to a
     >> small set of known types: double, int, string, bool, ...  The type used 
by
     >> the Boost library you are using is not among them, so you have to add 
code
     >> to
     >> map back and forth. Rcpp makes that easier; it is still far from 
automatic.
     >>
     >> R has packages such as Rmpfr interfacing GNU MPFR based on GMP. Maybe 
that
     >> is
     >> good enough?  Also note that Rcpp has a dedicated (low volume and 
friendly)
     >> mailing list where questions such as this one may be better suited.
     >>
     >> Cheers, Dirk
     >>
     >> [1] A slight generalisation. There are others but they are less common /
     >> not
     >> recommended.
     >>
     >> --
     >> dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
     >>

     > [[alternative HTML version deleted]]

     > ______________________________________________
     > R-package-devel@r-project.org mailing list
     > https://stat.ethz.ch/mailman/listinfo/r-package-devel

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to