Skip to content

Change in runif() behavior in Rcpp 0.12.16 #836

@kbroman

Description

@kbroman

With Rcpp 0.12.16, my simcross package started throwing errors in the tests. I don't fully understand what's going on, but it seems related to me calling runif(n,0,L) with n==0.

Consider the following code (also available as a gist). This simulates a Poisson process on the interval (0,L) by drawing a Poisson number of points and then drawing their positions from a uniform distribution. There are two versions of the function; in the first, I don't try to trap the n==0 cases; in the latter I call runif() only with n>0.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector sim_crossovers_orig(const double L)
{
    int n_xo;

    n_xo = R::rpois(L/100.0);

    NumericVector tmp = runif(n_xo, 0.0, L);
    return tmp.sort();
}

// [[Rcpp::export]]
NumericVector sim_crossovers_new(const double L)
{
    int n_xo;

    n_xo = R::rpois(L/100.0);

    NumericVector tmp(0);
    if(n_xo > 0) tmp = runif(n_xo, 0.0, L);
    return tmp.sort();
}

With Rcpp ver 0.12.15, these two functions give the same results.

Rcpp::sourceCpp("sim_crossovers.cpp")

set.seed(20180318)
z <- replicate(10000, sim_crossovers_orig(100))
table(sapply(z, length))
##    0    1    2    3    4    5    6
## 3661 3681 1837  631  151   35    4

set.seed(20180318)
z <- replicate(10000, sim_crossovers_new(100))
table(sapply(z, length))
##    0    1    2    3    4    5    6
## 3661 3681 1837  631  151   35    4

But with Rcpp ver 0.12.16, the former gives a ton of 0-length vectors, while the latter works fine, though it gives somewhat different results from ver 0.12.15. (The tables being printed should be 10,000 draws from a Poisson distribution with mean 1.)

Rcpp::sourceCpp("sim_crossovers.cpp")

set.seed(20180318)
z <- replicate(10000, sim_crossovers_orig(100))
table(sapply(z, length))
##    0    1    3
## 9994    5    1

set.seed(20180318)
z <- replicate(10000, sim_crossovers_new(100))
table(sapply(z, length))
##    0    1    2    3    4    5    6
## 3637 3698 1846  621  152   41    5

I've tried this in both Linux and MacOS (High Sierra), and with R 3.4.3 and 3.4.4, with the same results in all cases. (Just the version of Rcpp matters.)

Here's my session info in the Rcpp 0.12.16 case on my Mac:

R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] Rcpp_0.12.16    devtools_1.13.5

loaded via a namespace (and not attached):
[1] compiler_3.4.4   tools_3.4.4      withr_2.1.1.9000 memoise_1.1.0    digest_0.6.15

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions