Thank you Prof. Maechler! Indeed 0.1 is not the same as 1/10 and there are many ways to get more precise versions of it. I will look into how to take inputs as strings. Thank you!
Best, Khue Tran On Sat, Jul 20, 2024 at 4:40 AM Duncan Murdoch <murdoch.dun...@gmail.com> wrote: > Just for your info: > > > exp(mpfr("0.1", 152)) > 1 'mpfr' number of precision 152 bits > [1] 1.1051709180756476248117078264902466682245471948 > > So it looks like your approach of parsing the numbers yourself makes sense. > > You can find the R parser here: > > https://github.com/wch/r-source/blob/b64422334a8269535718efd9a1f969c94b103056/src/main/gram.y#L2577-L2705 > > I don't know how much of that you want to replicate, but I suppose > handling the weird cases (e.g. 0x1p1) the way R does will make it easier > for your users. > > Duncan Murdoch > > On 2024-07-18 4:29 p.m., Khue Tran wrote: > > Hi, > > > > I am trying to create an Rcpp package that involves arbitrary precise > > calculations. The function to calculate e^x below with 100 digits > precision > > works well with integers, but for decimals, since the input is a double, > > the result differs a lot from the arbitrary precise result I got on > > Wolfram. > > > > I understand the results are different since 0.1 cannot be represented > > precisely in binary with limited bits. It is possible to enter 1 then 10 > > and get the multiprecision division of these two integers to attain a > more > > precise 0.1 in C++, but this method won't work on a large scale. Thus, I > am > > looking for a general solution to get more precise inputs? > > > > The dummy example is as follows: > > > > library(Rcpp) > > > > sourceCpp(code = ' > > #include <boost/multiprecision/cpp_dec_float.hpp> > > #include <boost/math/special_functions/expm1.hpp> > > > > // [[Rcpp::depends(BH)]] > > // [[Rcpp::export]] > > std::string calculateExp100(double x) { > > boost::multiprecision::cpp_dec_float_100 bx = x; > > boost::multiprecision::cpp_dec_float_100 expx = exp(bx); > > return expx.str(50); > > } > > ') > > > > [1] represents the R output and [W] for Wolfram results > > > > calculateExp100(1) # Agrees with Wolfram answer all the way to the last > > digit > > [1] "2.7182818284590452353602874713526624977572470937" > > [W] 2.7182818284590452353602874713526624977572470936999595749669676277... > > > > calculateExp100(0.1) # Differs pretty significantly from Wolfram's answer > > [1] "1.1051709180756476309466388234587796577416634163742" > > [W] 1.1051709180756476248117078264902466682245471947375187187928632894... > > > > 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! > > > > Thank you! > > Best, > > Khue Tran > > > > [[alternative HTML version deleted]] ______________________________________________ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel