[Rcpp-devel] Rational number and the BH package

Dirk Eddelbuettel edd at debian.org
Thu Jul 10 19:35:52 CEST 2014


Here is a quick example for using the BH package from CRAN to get rational numbers:

R> sourceCpp("/tmp/ratEx.cpp")

R> invisible(quicktest())
Half is 1/2
Sum of two halves is 1/1

where the code in /tmp/ratEx.cpp follows below.  I enable C++11 merely for
convenience as R (and g++) otherwise complain about 'long long'. Should your
compile not like or support C++11, just remove the line with the cpp11 plugin.


#include <Rcpp.h>

// [[Rcpp::depends(BH)]]

#include <boost/rational.hpp>

// not needed but suppress 'long long' warnings from Boost
// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
bool quicktest() {

  boost::rational<int> half(1,2);

  Rcpp::Rcout << "Half is " << half << std::endl;
  Rcpp::Rcout << "Sum of two halves is " << half + half << std::endl;

  return true;

/*** R

http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org

More information about the Rcpp-devel mailing list