[Rcpp-devel] Rational number and the BH package

Robin Girard robin.girard at mines-paristech.fr
Thu Jul 10 23:48:28 CEST 2014

Thanks a lot, sorry for my question, it was indeed here in front of me ...

----- Mail original -----
De: "Dirk Eddelbuettel" <edd at debian.org>
À: "Dirk Eddelbuettel" <edd at debian.org>
Cc: "Robin Girard" <robin.girard at mines-paristech.fr>, "rcpp-devel" <rcpp-devel at lists.r-forge.r-project.org>
Envoyé: Jeudi 10 Juillet 2014 19:35:52
Objet: Re: [Rcpp-devel] Rational number and the BH package


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