Reputation: 33
I tried to write a simple function in cpp which I can use in rcpp, like:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
inline static double calc(double a, double b, double c){
return a - b + c;
}
// [[Rcpp::export]]
void other_function() {
double res = calc(1.00, 0.00, 3.00); // no error
NumericVector resV;
resV[0] = calc(1.00, 0.00, 3.00); // Floating point exception
Rcout << res; // Floating point exception
}
But every time if I try to access the result of the function calc() I got an Floating point exception.
If I only source the R File and call the funcion res() from the console everything seems to be fine:
source("myscript.R")
calc(1.00,0.00,3.00)
>> 4.00
What I already tried:
convert double to NumericVector res and only working with res[0] > not working
try float > not working
do the calculation direct in the other_function > same error
UPDATE:
Everytime I try to call other_function() in an other function I get the following message:
Floating point exception
For example:
void another_function() {
other_function(); // produce Floating point exception
}
If I call the other_function() directly in R everything is fine!
Also this code (thanks to snake_style) didn´t work for me:
NumericVector resV = NumericVector::create(progressionC(1.00, 0.00, 3.00));
I solved the problem by simple reinstall the Rcpp package. I don´t know why I got this error... anyway, now it works.
Upvotes: 1
Views: 790
Reputation: 26843
Not an answer but I want to store my failed attempts at reproducing this issue. I am using R 3.5.1 on Debian stable with gcc 6.3. I used the following code:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
inline static double calc(double a, double b, double c){
return a - b + c;
}
// [[Rcpp::export]]
void other_function() {
double res = calc(1.00, 0.00, 3.00); // no error
NumericVector resV;
resV[0] = calc(1.00, 0.00, 3.00); // Floating point exception
NumericVector resV1(1);
resV1[0] = calc(1.00, 0.00, 3.00); // Floating point exception
NumericVector resV2 = NumericVector::create(calc(1.00, 0.00, 3.00));
Rcout << res << "/" << resV << "/" << resV1 << "/" << resV2; // Floating point exception
}
// [[Rcpp::export]]
void another_function() {
other_function(); // produce Floating point exception
}
/*** R
calc(1.00, 0.00, 3.00)
other_function()
another_function()
*/
Calling Rcpp::sourceCpp()
compiles this code and produces the following output:
> calc(1.00, 0.00, 3.00)
[1] 4
> other_function()
4//4/4
> another_function()
4//4/4
calc
is not assigned (resV
outputs nothing).calc
is used with the static create
method as suggested by @snake_style.Upvotes: 1
Reputation: 1168
Try like this:
NumericVector resV = NumericVector::create(calc(1.00, 0.00, 3.00));
Upvotes: 1