[Rd] prod(numeric(0)) surprise

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jan 9 03:34:33 CET 2006


On 1/8/2006 9:24 PM, Ben Bolker wrote:
>    It surprised me that prod(numeric(0)) is 1.
> I guess if you say (operation(nothing) == identity
> element) this makes sense, but ??

What value were you expecting, or were you expecting an error?  I can't 
think how any other value could be justified, and throwing an error 
would make a lot of formulas more complicated.

> 
>     Looking in the code, this makes sense:
> basically (s=1; for i=0 to length(x),
> multiply s by x[i]) -- which comes out to 1.
> 
>    What *should* prod(numeric(0)) produce?
> I couldn't find the answer documented anywhere.
> 
>    (And how about sum(numeric(0))==0,
> which for some reason makes more intuitive sense
> to me, but is really exactly the same thing --
> consider exp(sum(log(numeric(0)))) ... ?)

That's a fairly standard mathematical convention, which is presumably 
why sum and prod work that way.

Duncan Murdoch



More information about the R-devel mailing list