# [Rd] qbinom returns NaN

Spencer Graves spencer.graves at pdf.com
Wed Nov 23 23:01:29 CET 2005

```Hi, All:

For most but not all cases, qbinom is the inverse of pbinom.
Consider the following example, which generates an exception:

> (pb01 <- pbinom(0:1, 1, .5, log=T, lower.tail=FALSE))
 -0.6931472       -Inf

Since "lower.tail=FALSE", Pr{X>1} = 0 in this context, and log(0) =
-Inf, consistent with the documentation.

However, the inverse of this does NOT recover 0:1:

> qbinom(pb01,1, .5, log=T, lower.tail=F)
   0 NaN

Shouldn't the NaN here be 1?  If yes, this is relatively easy to fix.
Consider for example the following:

qbinom. <-
function (p, size, prob, lower.tail = TRUE, log.p = FALSE){
q. <- .Internal(qbinom(p, size, prob, lower.tail, log.p))
q.[p==(-Inf)] <- 1
q.
}
> qbinom.(pb01,1, .5, log=T, lower.tail=F)
 0 1
Warning message:
NaNs produced in: qbinom(p, size, prob, lower.tail, log.p)

It's also easy to eliminate the Warning.  Consider for example the
following:

qbinom. <-
function (p, size, prob, lower.tail = TRUE, log.p = FALSE){
if(any(p.inf <- p==(-Inf))&&(!lower.tail)&&log.p){
n <- max(length(p), length(size), length(prob))
p <- rep(p, length=n)
size <- rep(size, length=n)
prob <- rep(prob, length=n)
q. <- size
q.[p>(-Inf)] <- .Internal(qbinom(p[!p.inf],
size[!p.inf], prob[!p.inf], lower.tail, log.p))
return(q.)
}
.Internal(qbinom(p, size, prob, lower.tail, log.p))
}

I suspect that for the right person, it would likely be easy to fix
this in the .Internal qbinom code.  However, that's beyond my current R
skill level.

Thanks for all your efforts to make R what it is today.
Best Wishes,
spencer graves

--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915

```