I know there are methods to calculate a confidence interval for a proportion to keep the limits within (0, 1), however a quick Google search lead me only to the standard calculation: $\hat{p} \pm 1.96*\sqrt\frac{\hat{p}(1-\hat{p})}{N}$. I also believe there is a way to calculate the exact confidence interval using the binomial distribution (example R code would be nice). I know I can use the prop.test function to get the interval but I'm interested in working through the calculation.
Sample situations (N = number of trials, x = number of success):
N=40, x=40
N=40, x=39
N=20, x=0
N=20, x=1
Best Answer
Use a Clopper-Pearson interval?
Wikipedia discribes how to do this here: http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
For example if you take your 39 successes in 40 trial example you get:
For your 40 out of 40 you get: