# Re: [Rd] Recycling in arithmetic operations involving zero-length vectors

```Actually, sum(), all(), any(), prod() all return a _carefully considered_
result in the case of an empty vector. The sum over a disjoint union of sets
should be the sum of the sums over each set, so if a set is empty, its
contribution to the sum must be zero. Similarly, the product over an empty set
is 1. Logical aggregate all() should be stable for "&" and any() for "|", hence
all(logical(0))==TRUE and any(logical(0))==FALSE. ```
```
-pd

> On 16 Jan 2023, at 16:53 , GILLIBERT, Andre <andre.gillib...@chu-rouen.fr>
> wrote:
>
> Duncan Murdoch <murdoch.dun...@gmail.com> wrote:
>
>> To even do that, we would have to first decide which "cases" should produce
>> a warning.
>
>> Let's say `1 + x` should give a warning when x = numeric(0). Then should
>> `x^2` also produce a warning? Should `x^0.5`? Should `sqrt(x)`?
>> Should `log(x)`?
>
>
> The most probable errors would be in functions taking two arguments (e.g.
> `+`) and for which one argument has length >= 2 while the other has length 0.
>
> In my experience, most code with accidental zero-length propagations (e.g.
> typo in data_frame\$field) quickly lead to errors, that are easy to debug
> (except for beginners), and so, do not need a warning.
>
> The only cases where zero-length propagation is really dangerous in my
> experience is in code using an aggregating function like sum(), all() or
> any(), because it silently returns a valid value for a zero-length argument.
> Emitting warnings for sum(numeric(0)) would probably have too many false
> positives but a (length >= 2) vs (length == 0) warning for common binary
> operators could sometimes catch the issue before it reaches the aggregating
> function.
>
> --
> Sincerely
> André GILLIBERT
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd....@cbs.dk  Priv: pda...@gmail.com

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
```