[R] Precision in estimating log

Sean O'Riordain seanpor at acm.org
Fri May 19 14:32:39 CEST 2006


Anthony,
in the same way that we're not allowed to say if(x==0) if x is a real
number, we can't say that 0.05=1-0.95... as 1-0.95 is not represented
as a base 10 number on the computer, but in some base 2^i depending on
your computer...and the representation is not necessarily exact...
i.e. one-third (1/3) isn't representable exactly as a decimal number,
but I'd guess that it is in some other base...

I know it only answers part of your question... but perhaps that helps?
cheers,
Sean


On 19/05/06, Gichangi, Anthony <anthony at stat.sdu.dk> wrote:
> Hi R-users,
>
> I have the following code:
>
> f <-function(x,p)sqrt(-(x^2)-2*log(1-p))
>
> r1 <-sqrt(-2*log(1-0.95))
>
> r2 <-sqrt(-2*log(0.05))
>
> on executing i get the following results
>
> > f(r1,0.95)
> [1] 0
> >
> > f(r2,0.95)
> [1] NaN
> Warning message:
> NaNs produced in: sqrt(-(x^2) - 2 * log(1 - p))
>
> I tried to track the problem and found that the answer to
> log(0.05) is different from the answer to log(1-0.95) which
> is ofcourse not true and hence it causes problems in the code
>
>  print(log(0.05),digit=22)
>
> [1] -2.9957322735539909
> > print(log(1-0.95),digit=22)
>
> [1] -2.99573227355399
>
>
> Any possible explanation ?
>
> Regards
> Anthony
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list