[Rcpp-devel] A question in slides from Rcpp workshop

Dirk Eddelbuettel edd at debian.org
Fri Aug 5 18:21:44 CEST 2011


Zhongyi,

On 5 August 2011 at 04:05, Zhongyi Yuan wrote:
| Hi Douglas,
| 
| Thank you for the reply. That clarifies quite a bit.
| But still, it does not explain the different behavior of those examples, does
| it? One moment ago, I was thinking that the L suffix might have made it
| constant and hence can't be modified. Looks like I am wrong. (And fun(1:3)
| gives the same result too.)

Don't take this the wrong way but you man know too little R to get to what
this is about. But please consider this difference:

R> typeof(1L)
[1] "integer"
R> typeof(1)
[1] "double"
R> 

In particular, both '1' and '1.0' get you a double.

We added the example to show that when you use 1L:3L (eg an integer vector)
and assign to a Rcpp::NumericVector you _do get a separate copy_ due to C++
casting semantics.  The proper lightweight call is seq(1.0, 3.0, by=1.0)
which then exhibits the shallow copy vs deep copy issue the rest of the
example is about.

This is somewhat advanced corner-case stuff so maybe you should not fret too
too much about it now but come back to it later.  

Dirk
 
| Zhongyi
| 
| On Fri, Aug 5, 2011 at 3:49 AM, Douglas Bates <bates at stat.wisc.edu> wrote:
| 
|     On Fri, Aug 5, 2011 at 5:27 AM, Zhongyi Yuan <yuanzygoso at gmail.com> wrote:
|     > Dear useR's,
|     >
|     > After I found Rcpp a few days ago, I've been very excited collecting
|     > documents for learning. But still I find myself understand little.
|     > Here's a question I want you to help me with.
|     >
|     > In page 17 of Dirk and Romain's slides from part2 of the Apr 28 Rcpp
|     > workshop (here's a link:
|     > http://dirk.eddelbuettel.com/papers/rcpp_workshop_part_2_details.pdf), I
|     > can't figure out why the two examples behave differently.
|     > And also, why are people using 1L:3L? What not just 1:3?
| 
|     It happens that they are the same but only because 1:3 generates an
|     integer sequence by default.  Most of the time 1 gives a double
|     precision floating point number whereas 1L is an integer.  Those with
|     long-time experience in writing R code tend to use the L when they
|     know that an integer is wanted, just to bypass the conversion step.
| 
|     > Maybe I am asking silly question? But please do help me. I couldn't find
|     an
|     > answer on google.
|     > Thank you.
|     >
|     > Best,
|     > Zhongyi
|     >
|     >
|     > _______________________________________________
|     > Rcpp-devel mailing list
|     > Rcpp-devel at lists.r-forge.r-project.org
|     > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
|     >
|     >
| 
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list