From john at johnmaddock.co.uk  Thu May 31 20:04:25 2007
From: john at johnmaddock.co.uk (John Maddock)
Date: Thu, 31 May 2007 19:04:25 +0100
Subject: [Rd] Problems when linking to R shared library
Message-ID: <007501c7a3ae$25c2f840$38540352@fuji>

Folks,

I'm fairly sure that I'm doing something stupid, but I'm getting a few
really strange results from *some* of the distributions, but by no means 
all,
when I link directly to the R shared library.

I've tried this on both Windows with the precompiled Mingw binary of R-2.5.0
(compiling my code with MinGW-3.4.2), and by building R-2.5.0 on Mandriva
Linux with gcc-3.4.4 and --enable-R-shlib.  The actual code is at the end of
the message, but the main cases are:

std::cout << pf(1.5, 2.0, 3.0, 1, 0) << std::endl;

Outputs 1, expect 0.646447

std::cout << pchisq(1.5, 2.0, 1, 0) << std::endl;

Outputs 1, expect 0.527633

std::cout << pgamma(1.5, 2.0, 3.0, 1, 0) << std::endl;

Outputs 1, expect 0.090204

std::cout << ppois(2.0, 5.0, 1, 0) << std::endl;

Outputs 0, expect 0.124652

std::cout << qchisq(0.5, 2.0, 1, 0) << std::endl;

Outputs -0.61379, expect 1.38629

std::cout << qgamma(0.5, 2.0, 3.0, 1, 0) << std::endl;

Outputs 0.0282703, expect 5.03504

I should stress that within the R environment, I *do* get the values I
expect: though sometimes the arguments need adjusting as the C functions
have slightly different argument lists from the R versions.

Also:

std::cout << qpois(0.5, 5.0, 1, 0) << std::endl;

Seems to go into an infinite loop inside qpois, while:

qbeta(0.1, 1e-5, 1e-5, 1, 0);

Raises an access violation inside R: although I should stress that all other
beta quantiles I've tested come out OK.

Anyway, hopefully you can just tell me what an idiot I am :-)

Thanks in advance for any help you can give,

John Maddock.

The complete source code is:

#include <iostream>
#include <iomanip>

extern "C" {
#include "Rmath.h"
}

int main(int argc, const char** argv)
{
  std::cout << pf(1.5, 2.0, 3.0, 1, 0) << std::endl;
  std::cout << pchisq(1.5, 2.0, 1, 0) << std::endl;
  std::cout << pgamma(1.5, 2.0, 3.0, 1, 0) << std::endl;
  std::cout << ppois(2.0, 5.0, 1, 0) << std::endl;
  std::cout << qchisq(0.5, 2.0, 1, 0) << std::endl;
  std::cout << qgamma(0.5, 2.0, 3.0, 1, 0) << std::endl;
  //std::cout << qpois(0.5, 5.0, 1, 0) << std::endl;
  return 0;
}



[Rd] Problems when linking to R shared library
John Maddock