prod(0, 1:1000) ; 0 * Inf etc
I definitely do agree with you.
Basically, I see two different ways to proceed:
1. one could first check if there are any 0 in the vector and then
return 0 without computing the product
2. or convert prod(x1, x2, x3) in prod(c(x1, x2, x3))
Both approaches are similar except for the pathological case where one
vector x1 is really huge. An example:
prod(0, 1:1e25)
1. will give 0
2. will give an error stating that the vector c(0, 1:1e255) will be
too large - in length I mean
Consequently, my opinion will be that approach 1 will be better - and
maybe faster because it'll avoide useless computations.
Best,
Mathieu
Martin Maechler a ?crit :
I think most of us would expect prod(0:1000) to return 0, and ...
... it does.
However, many of us also expect
prod(x1, x2) to be equivalent to
prod(c(x1,x2))
the same as we can expect that for min(), max(), sum() and such
members of the "Summary" group.
Consequently, prod(0, 1:1000) should also return 0,
but as you see, it gives NaN which may be a bit puzzling...
The explanation is relatively simple:
1) The internal implementation uses
prod(x1, x2) := prod(x1) * prod(x2)
which in this case is
2) 0 * Inf and that is not 0, but NaN;
not necessarily because we would want that, but I think just
because the underlying C math library does so.
I would personally like to change both behaviors,
but am currently only proposing to change prod() such as to
return 0 in such cases.
This would be S-plus compatible, in case that matters.
Opinions?
Martin Maechler, ETH Zurich & R-core
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Institute of Mathematics Ecole Polytechnique F?d?rale de Lausanne STAT-IMA-FSB-EPFL, Station 8 CH-1015 Lausanne Switzerland http://stat.epfl.ch/ Tel: + 41 (0)21 693 7907