Skip to content

[Rcpp-devel] rpois returns double?

3 messages · Christian Gunning, R. Michael Weylandt, William Dunlap

#
Quick question, mainly out of curiousity.  I get that Rcpp uses the R api
on the backend, and in the R api, R::rpois returns a double (and
Rcpp::rpois returns NumericVector). The R C code doesn't offer much in the
way of explanation of why double is returned, but always returns a floor() (
https://svn.r-project.org/R/trunk/src/nmath/rpois.c).

It seems to me that, in this case, upstream could (should?) cast to int.
Since it doesn't, downstream users like Rcpp and me must choose whether to
follow upstream convention or to cast.  In this case, is the "best
practice" to cast-to-int right away, or to follow along and just use
doubles for everything?

I've run into this recently with other RNG code that returns doubles for
everything, and I'm wondering if there's an obvious tradeoff that I'm
missing, like speed versus type-correctness?  If anyone has any insights,
I'd love to hear them.

thanks,
Christian
#
The set of integers exactly representable by a double is a superset of those which can represented by a 32bit int.

http://stackoverflow.com/questions/759201/representing-integers-in-doubles
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150425/24ce8b24/attachment.html>
#
Note that the R function rpois() always returns an integer vector and thus
commonly runs into problems when lambda is near or above 2^31 (the
smallest positive integral double that cannot represented as a 32-bit signed
integer).
[1] 2147454617         NA         NA         NA 2147412285         NA
    NA
 [8]         NA 2147469496 2147446621
Warning message:
In rpois(10, 2^31) : NAs produced
[1] "integer"



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Apr 25, 2015 at 9:33 AM, Michael Weylandt <
michael.weylandt at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150425/37c5f5f6/attachment.html>