4

I'm just starting to use Rcpp so sorry if I'm missing an easy step or something similar... I have tried this from ?sourceCpp

library(Rcpp)
sourceCpp(code='
  #include <Rcpp.h>

  // [[Rcpp::export]]
  int fibonacci(const int x) {
    if (x == 0) return(0);
    if (x == 1) return(1);
    return (fibonacci(x - 1)) + fibonacci(x - 2);
  }'
)

Up to fibonacci(46) everything's fine, but then I get:

> fibonacci(47)
[1] -1323752223
> fibonacci(48)
[1] 512559680
> fibonacci(49)
[1] -811192543
> fibonacci(50)
[1] -298632863

According to this page the above should be:

47 : 2971215073
48 : 4807526976
49 : 7778742049
50 : 12586269025

Do you get the same result?

Michele
  • 8,563
  • 6
  • 45
  • 72
  • +1 for a self-contained reproducible example. Very nice. – Simon O'Hanlon Sep 18 '13 at 16:40
  • @SimonO101 Thanks for the +1 (always accepted) but to be honest I just copied and pasted from `?sourceCpp`. Even though I could probably have done the fibonacci function by myself I'm less then basic in the C :-( – Michele Sep 18 '13 at 16:43
  • Also, you don't need the `#include` which gets added (along with other scaffolding) by `sourceCpp()`. – Dirk Eddelbuettel Sep 18 '13 at 16:45
  • @DirkEddelbuettel oh I see, thanks. Like I said, I've literally started yesterday, so for now I was just following the docs (and code from your workshops) exactly as they are, and even doing so some of them don't compile... question coming... – Michele Sep 18 '13 at 16:53
  • @DirkEddelbuettel then perhaps hadley should update [**his guide**](http://adv-r.had.co.nz/Rcpp.html#using-sourcecpp)... ? – Simon O'Hanlon Sep 18 '13 at 16:58
  • Maybe you should tell that _him_ rather than me? – Dirk Eddelbuettel Sep 18 '13 at 17:02
  • @DirkEddelbuettel No, I'm asking, hence the `?` – Simon O'Hanlon Sep 18 '13 at 17:02
  • Actually, nevermind, I confused myself over the _string_ use with `sourceCpp()` which is unusual. I'd define a Fibonacci function in one long line via `cppFunction()`. – Dirk Eddelbuettel Sep 18 '13 at 17:03
  • @DirkEddelbuettel ok, that's more in line with what I thought [from following *his* guide! :-) ] – Simon O'Hanlon Sep 18 '13 at 17:04
  • 2
    For completeness: `cppFunction('double fib(double x) { if (x<2) return x; else return fib(x-1)+fib(x-2); }')` – Dirk Eddelbuettel Sep 18 '13 at 17:06
  • @DirkEddelbuettel just a pure curiosity: `Rcpp` is yours, right? But you say " I'd define a Fibonacci ..." like you mean differently, so I guess someone else wrote the R Doc for `Rcpp` – Michele Sep 18 '13 at 17:09

1 Answers1

5

You are exceeding the maximum limit for signed integers (technically this would be a long int I guess). Use double instead...

library(Rcpp)
sourceCpp(code='
  #include <Rcpp.h>

  // [[Rcpp::export]]
  double fibonacci(const double x) {
    if (x == 0) return(0);
    if (x == 1) return(1);
    return (fibonacci(x - 1)) + fibonacci(x - 2);
  }'
)

fibonacci(47)
#[1] 2971215073
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • Oh.. ok. I actually replaced `int` with `long` and it gave the same number... But I didn't think about double I thought long was enough... Thanks anyway! – Michele Sep 18 '13 at 16:36
  • 1
    `int` and `long` are the same size. But even if you used a larger integer type, R's integer type is itself only so big. – Peyton Sep 18 '13 at 16:37
  • @Peyton ok thanks I thought int was 16 bit and long 32 ... my bad! Thanks guys. – Michele Sep 18 '13 at 16:40
  • 1
    @Michele, it depends: http://stackoverflow.com/questions/589575/size-of-int-long-etc – Peyton Sep 18 '13 at 16:56
  • 1
    Note: R only has `int` and `double` types. If you need extra precision on your integers, you're stuck with `double`, unless you use an addon package (eg [int64](http://cran.r-project.org/web/packages/int64/vignettes/int64.pdf)) – Kevin Ushey Sep 18 '13 at 18:56