[Rcpp-commits] r3306 - pkg/Rcpp/inst/doc/Rcpp-FAQ
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Nov 7 13:22:40 CET 2011
Author: romain
Date: 2011-11-07 13:22:39 +0100 (Mon, 07 Nov 2011)
New Revision: 3306
Modified:
pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw
Log:
update the vignette to show int64_t usage
Modified: pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw 2011-11-07 12:11:23 UTC (rev 3305)
+++ pkg/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw 2011-11-07 12:22:39 UTC (rev 3306)
@@ -583,40 +583,33 @@
@
%
-\subsection{Why can long long types not be cast correctly?}
+\subsection{Can I use 64 bits integer types ?}
-That is a good and open question. We rely on the basic \proglang{R} types,
-notably \code{integer} and \code{numeric}. These can be cast to and from
-\proglang{C++} types without problems. But there are corner cases. The
-following example, contributed by a user, shows that we cannot reliably cast
-\code{long} types (on a 64-bit machines).
+Yes. Since version 0.9.8, \pkg{Rcpp} depends on the \pkg{int64}
+package. \pkg{int64} brings 64 bit integer vectors to R, with the
+two S4 classes \code{int64} and \code{uint64} that internally
+handle C++ types \code{int64\_t} and \code{uint64\_t}.
-<<eval=FALSE>>=
+\pkg{Rcpp}'s \code{wrap} function is able to handle the
+primitives \code{int64\_t} and \code{uint64\_t} as well as containers
+of these:
+
+<<>>=
BigInts <- cxxfunction(signature(),
- 'std::vector<long> bigints;
+ 'std::vector<int64_t> bigints;
bigints.push_back(12345678901234567LL);
bigints.push_back(12345678901234568LL);
Rprintf("Difference of %ld\\n", 12345678901234568LL - 12345678901234567LL);
- return wrap(bigints);', plugin="Rcpp", includes="#include <vector>")
+ return wrap(bigints);',
+plugin="Rcpp" )
retval<-BigInts()
-
-# Unique 64-bit integers were cast to identical lower precision numerics
-# behind my back with no warnings or errors whatsoever. Error.
-
-stopifnot(length(unique(retval)) == 2)
+retval
@
-%
-While the difference of one is evident at the \proglang{C++} level, it is no
-longer present once cast to \proglang{R}. The 64-bit integer values get cast
-to a floating point types with a 53-bit mantissa. We do not have a good
-suggestion or fix for casting 64-bit integer values: 32-bit integer values
-fit into \code{integer} types, up to 53 bit precision fits into
-\code{numeric} and beyond that truly large integers may have to converted
-(rather crudely) to text and re-parsed. Using a different representation as
-for example from the \href{http://gmplib.org/}{GNU Multiple Precision Arithmetic
- Library} may be an alternative.
+The vignette in the \pkg{int64} package has the details on how 64 bit
+integer types are handled. In short, we split the higher bits and the lower
+into two integers.
\section{Support}
More information about the Rcpp-commits
mailing list