Search code examples
rdecimalrcpptruncatedigits

Rcpp C++ truncates decimal places no matter what


  • Platform: Redhat Enterprise 7.5 on AWS instance with RStudio Server.
  • Reproducible Code: I apologize in advance for not providing code because if I simplify the function into a reproducible function, I know it will work and produce the expected results.
  • Case:
  • I have a function coded in C++ using Rcpp, the function includes a division by the product of some subtraction of very small quantities. For example, it first executes some subtraction like (.0012345678 - .0012345677), and then based on this example, the function should divide by .0000000001, but the function truncates the quantities in the subtraction (.001234 - .001234) and produces zero and consequently inf as an output of the division by zero.
    • My question is: is there anything that I should be aware of in Rcpp setting to get it to produce at least 15 decimal places so that the subtraction does not produce inf or nan. I have coded another much smaller function and it works just fine. However, no matter what I do, the much bigger function truncates the double data types numbers going into the division and the subtraction.

Solution

  • Please see R FAQ 7.31 and there reference therein -- you are at the "epsilon" precision limit here.

    Rcpp uses the same double type as R and every other C-based program. There is also long double, but what you may really want is an arbitrary / multiple precision library such as GNU mpfr which also has an R package Rmpfr.