[Rd] prod(0, 1:1000) ; 0 * Inf etc
Martin Maechler
maechler at stat.math.ethz.ch
Mon Apr 21 18:40:43 CEST 2008
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
More information about the R-devel
mailing list