Skip to content

[Rcpp-devel] sugar: x+y and y+x gives different results when there are NA's in y

5 messages · Romain Francois, Søren Højsgaard

#
Dear all,
Came across this:

#include <Rcpp.h>
using namespace Rcpp;

//[[Rcpp::export]]
List  sumxy(NumericVector x, NumericVector y){
  NumericVector xy = x+y;
  NumericVector yx = y+x;
  return List::create(xy, yx);
}

/*** R
x <- c(1,3,5,7)
y <- c(NA,2,4,6,8)
x+y
y+x
sumxy(x, y)
 */
[1] NA  5  9 13  9
[1] NA  5  9 13  9
[[1]]
[1] NA  5  9 13
[[2]]
[1] NA  5  9 13  8

Is this intentional? Apologies if this has been documented anywhere; was unable to find it.

Regards
S?ren
#
This has nothing to do with NA. It's just about the size difference. sugar does not make attempt at recycling. Your responsibility. 

Romain
#
OK; thanks! Shall I read this such as "the behaviour is undefined"?
S?ren

|-----Original Message-----
|From: Romain Francois [mailto:romain at r-enthusiasts.com]
|Sent: 22. november 2014 13:25
|To: S?ren H?jsgaard
|Cc: rcpp-devel at lists.r-forge.r-project.org
|Subject: Re: [Rcpp-devel] sugar: x+y and y+x gives different results when
|there are NA's in y
|
|This has nothing to do with NA. It's just about the size difference.
|sugar does not make attempt at recycling. Your responsibility.
|
|Romain
|
|> Le 22 nov. 2014 ? 11:48, S?ren H?jsgaard <sorenh at math.aau.dk> a ?crit :
|>
|> Dear all,
|> Came across this:
|>
|> #include <Rcpp.h>
|> using namespace Rcpp;
|>
|> //[[Rcpp::export]]
|> List  sumxy(NumericVector x, NumericVector y){  NumericVector xy =
|> x+y;  NumericVector yx = y+x;  return List::create(xy, yx); }
|>
|> /*** R
|> x <- c(1,3,5,7)
|> y <- c(NA,2,4,6,8)
|> x+y
|> y+x
|> sumxy(x, y)
|> */
|>
|>> x <- c(1,3,5,7)
|>> y <- c(NA,2,4,6,8)
|>> x+y
|> [1] NA  5  9 13  9
|>> y+x
|> [1] NA  5  9 13  9
|>> sumxy(x, y)
|> [[1]]
|> [1] NA  5  9 13
|> [[2]]
|> [1] NA  5  9 13  8
|>
|> Is this intentional? Apologies if this has been documented anywhere;
|was unable to find it.
|>
|> Regards
|> S?ren
|>
|>
|> _______________________________________________
|> 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-deve
|> l
#
You get UB if the first is bigger than the second. 

Envoy? de mon iPhone
#
Thanks. May I suggest adding a remark about this in the sugar-vignette (if it is not already there - in which case I apologize for not having seen it!)

Cheers
S?ren

|-----Original Message-----
|From: Romain Francois [mailto:romain at r-enthusiasts.com]
|Sent: 22. november 2014 14:36
|To: S?ren H?jsgaard
|Cc: rcpp-devel at lists.r-forge.r-project.org
|Subject: Re: [Rcpp-devel] sugar: x+y and y+x gives different results when
|there are NA's in y
|
|You get UB if the first is bigger than the second.
|
|Envoy? de mon iPhone
|
|> Le 22 nov. 2014 ? 14:30, S?ren H?jsgaard <sorenh at math.aau.dk> a ?crit :
|>
|> OK; thanks! Shall I read this such as "the behaviour is undefined"?
|> S?ren
|>
|> |-----Original Message-----
|> |From: Romain Francois [mailto:romain at r-enthusiasts.com]
|> |Sent: 22. november 2014 13:25
|> |To: S?ren H?jsgaard
|> |Cc: rcpp-devel at lists.r-forge.r-project.org
|> |Subject: Re: [Rcpp-devel] sugar: x+y and y+x gives different results
|> |when there are NA's in y
|> |
|> |This has nothing to do with NA. It's just about the size difference.
|> |sugar does not make attempt at recycling. Your responsibility.
|> |
|> |Romain
|> |
|> |> Le 22 nov. 2014 ? 11:48, S?ren H?jsgaard <sorenh at math.aau.dk> a
|?crit :
|> |>
|> |> Dear all,
|> |> Came across this:
|> |>
|> |> #include <Rcpp.h>
|> |> using namespace Rcpp;
|> |>
|> |> //[[Rcpp::export]]
|> |> List  sumxy(NumericVector x, NumericVector y){  NumericVector xy =
|> |> x+y;  NumericVector yx = y+x;  return List::create(xy, yx); }
|> |>
|> |> /*** R
|> |> x <- c(1,3,5,7)
|> |> y <- c(NA,2,4,6,8)
|> |> x+y
|> |> y+x
|> |> sumxy(x, y)
|> |> */
|> |>
|> |>> x <- c(1,3,5,7)
|> |>> y <- c(NA,2,4,6,8)
|> |>> x+y
|> |> [1] NA  5  9 13  9
|> |>> y+x
|> |> [1] NA  5  9 13  9
|> |>> sumxy(x, y)
|> |> [[1]]
|> |> [1] NA  5  9 13
|> |> [[2]]
|> |> [1] NA  5  9 13  8
|> |>
|> |> Is this intentional? Apologies if this has been documented
|> |> anywhere;
|> |was unable to find it.
|> |>
|> |> Regards
|> |> S?ren
|> |>
|> |>
|> |> _______________________________________________
|> |> 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-d
|> |> eve
|> |> l
|>