Duncan Murdoch
2023-Jan-16 11:11 UTC
[Rd] Recycling in arithmetic operations involving zero-length vectors
On 16/01/2023 5:23 a.m., Roland Fu? wrote:> Dear R-core, > > The language definition is very clear: > > "As from R 1.4.0, any arithmetic operation involving a zero-length > vector has a zero-length result." > > Thus, `1 + numeric()` returns `numeric(0)`. However, I don't find this > very intuitive because usually the shorter vector is recycled to the > length of the longer vector. Would it be possible to throw at least a > warning for such cases? I don't expect them to be intended by most users. > > Best regards,The previous paragraph says "If the length of the longer vector is not a multiple of the shorter one, a warning is given." Since 1 is not a multiple of 0, that implies a warning should be given here. However, R 1.4.0 was released more than 20 years ago, so I would guess there are lots of packages intentionally using this. For example, it's a way to propagate bad inputs through a long calculation that allows a single test at the end. And even unintentional uses are unlikely to lead to problematic results: numeric(0) is usually a pretty clear signal that something is wrong. So I'd suggest a documentation change: "As from R 1.4.0, any arithmetic operation involving a zero-length vector has a zero-length result *without a warning*." Duncan Murdoch
David Winsemius
2023-Jan-16 11:55 UTC
[Rd] Recycling in arithmetic operations involving zero-length vectors
Sent from my iPhone> On Jan 16, 2023, at 6:11 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > > ?On 16/01/2023 5:23 a.m., Roland Fu? wrote: >> Dear R-core, >> The language definition is very clear: >> "As from R 1.4.0, any arithmetic operation involving a zero-length >> vector has a zero-length result." >> Thus, `1 + numeric()` returns `numeric(0)`. However, I don't find this >> very intuitive because usually the shorter vector is recycled to the >> length of the longer vector. Would it be possible to throw at least a >> warning for such cases? I don't expect them to be intended by most users. >> Best regards, > > The previous paragraph says "If the length of the longer vector is not a multiple of the shorter one, a warning is given." Since 1 is not a multiple of 0, that implies a warning should be given here. > > However, R 1.4.0 was released more than 20 years ago, so I would guess there are lots of packages intentionally using this. For example, it's a way to propagate bad inputs through a long calculation that allows a single test at the end. > > And even unintentional uses are unlikely to lead to problematic results: numeric(0) is usually a pretty clear signal that something is wrong. > > So I'd suggest a documentation change: "As from R 1.4.0, any arithmetic operation involving a zero-length vector has a zero-length result *without a warning*."I doubt that a documentation change will help very much. Roland is responding here with his and my surprise at the lack of a warning after witnessing my answer to an R newb Q where the impression at seeing ?numeric(0) was understood as the value 0. I suggested that many interpretations were possible and that a warning was given for NA generation. I stand with Roland in thinking a warning is appropriate. David Winsemius> > Duncan Murdoch > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel