s-news
[Top] [All Lists]

Re: factorial function

To: J Liang <jl78150@yahoo.com>
Subject: Re: factorial function
From: Spencer Graves <spencer.graves@PDF.COM>
Date: Wed, 19 Mar 2003 20:47:07 -0800
Cc: s-news@lists.biostat.wustl.edu
References: <20030320030216.61674.qmail@web13702.mail.yahoo.com>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01
Did you step through the code one step at a time? When I did this, I saw the following:

> wasInteger
[1] F
> result[wasInteger]
numeric(0)
> round(result[wasInteger])
numeric(0)

Then the command

                result[wasInteger] <- round(result[wasInteger])

assigns a numeric vector of length 0 to a numeric vector of length 0, leaving the components of nonzero length of result unchanged.

The point is that "wasInteger" was a logical vector of length 1 with value F, and therefore result[F] is a numeric vector of length 0.

I also tried the following:

> result[0]
numeric(0)
> result[0] <- 1
> result
[1] 287.8853
>

This surprises me more, but I will let others comment on this convention.

Hope this helps.
Spencer Graves



J Liang wrote:
Hi, all
I am a newbie at s-plus, I am using splus 6.1, windows
xp.  There is some thing I could not explain about a
system function factorial, maybe there is something
fundatmental about s-plus I am not clear.  I would
appreciate any help.

When reading the following code of the system function
factorial, my understanding is that after caculate the
result, the function then goes on checking to see if n
is an integer, if it is, it would round the result and
assign it back to result.  Therefore, if it is not an
integer, the wasInteger parameter would be F, which is
coersed to be 0 in the next step.  With the indexing
of vector started at 1, result[0] should not have anything returned. However,
if I give 5.5 or another n with a decimal , I would
have a decimal returned value. factorial(5.5) gives
the following output:287.8853

here is the system function:
factorial <-function(n)
{
        result <- gamma(n + 1)
        wasInteger <- !(n %% 1) & !is.na(n)
        result[wasInteger] <- round(result[wasInteger])
        result
}

Thanks for any help.






__________________________________________________
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



<Prev in Thread] Current Thread [Next in Thread>