[Rd] The following code (using rgamma) hangs

Faheem Mitha faheem at faheem.info
Fri Jan 27 22:03:05 CET 2012


Hi,

I'm seeing something that may be a bug in R's standalone math library, 
which is packaged by Debian as r-mathlib. I reported it to the Debian BTS 
as http://bugs.debian.org/657573

I'm using Debian squeeze, and the code was tested with r-mathlib 2.11.1-6 
(default on stable) and 2.14.1-1 (from testing/unstable).

I summarize this report below. The following code with the R math library 
hangs. Note that set_seed is defined as taking two unsigned int arguments, 
so 0's are valid arguments. I'm guessing that since 0 is as low as an 
unsigned integer can go, it represents some kind of edge case.

################################################
#define MATHLIB_STANDALONE
#include <Rmath.h>

int main(void)
{
   set_seed(0, 0);
   rgamma(1, 1);
}
################################################

If I add the definitions of `get_seed` and `set_seed` from 
`src/nmath/standalone/sunif.c` to my code, then the hang disappears.

################################################
#define MATHLIB_STANDALONE
#include <Rmath.h>

/* A version of Marsaglia-MultiCarry */

static unsigned int I1=1234, I2=5678;

void set_seed(unsigned int i1, unsigned int i2)
{
     I1 = i1; I2 = i2;
}

void get_seed(unsigned int *i1, unsigned int *i2)
{
     *i1 = I1; *i2 = I2;
}

int main(void)
{
   set_seed(0, 0);
   rgamma(1, 1);
}
################################################

I assume sunif.c defines the `get_seed` and `set_seed` for the R 
standalone random number generation facilities.

However, I wonder why

a) redefining them in my source file makes the hang go away

and

b) why doesn't redefining `get_seed` and `set_seed` (even with the same 
definition) give a linker error, since the function has been defined in 
the library already?

Dirk also pointed out (in the bug report) that you get the following

##########################################################
int main(void)
{
     set_seed(0, 0);
     cout << "one normal " << norm_rand() << endl;
}
##########################################################

edd at max:/tmp$ g++ -o faheem faheem.cpp -lRmath; ./faheem
one normal -inf

One would expect norm_rand to return finite values, even in edge cases.

If you want me to report this as a bug, let me know. Thanks.

                                                        Regards, Faheem



More information about the R-devel mailing list