[R] -log10 of 0

peter dalgaard pdalgd at gmail.com
Sun Aug 28 19:40:36 CEST 2011


On Aug 28, 2011, at 18:12 , (Ted Harding) wrote:
> 
>  -log10(1/(10^307))
>  # [1] 307
>  -log10(1/(10^308))
>  # [1] 308
>  -log10(1/(10^309))
>  # [1] Inf
> 
> Note that the above forces R to compute the number before
> applying log10() to it. You can get a bit further with:
> 
>  -log10(1e-322)
>  # [1] 322.0052
>  -log10(1e-323)
>  # [1] 323.0052
>  -log10(1e-324)
>  # [1] Inf
>  -log10(1e-325)
>  # [1] Inf
> 
> which may have something to do with R parsing the expression
> before applying log10() to it (I don;t know).

That's a red herring:

> log10(10^-323)
[1] -323.0052
> log10(1e309)
[1] Inf

It's just that the situation is not quite symmetric between large and small values. This is because R (and the underlying FP support) allows denormalized numbers. 

Quick and somewhat imprecise FP tutorial (if you care about the very final bits of the FP double, I'm sure Google gets you there soon enough):

Floating numbers are usually stored as sign*0.1.......*2^y, where the dots represent 52 more binary digits and y is between -1023 and +1023. I.e. the first digit of the mantissa is a 1. However, in a denormalized value, the mantissa can be 0.0...01..... allowing a somewhat more graceful transition to zero at the expense of relative accuracy. This effectively buys you the 15 extra orders of magnitude, but only for small numbers. 

Notice that the granularity of these very small numbers is quite coarse: 

> 1e-323 - 1.5e-323
[1] -4.940656e-324
> 1e-323 - 1.4e-323
[1] -4.940656e-324
> 1e-323 - 1.3e-323
[1] -4.940656e-324
> 1e-323 - 1.2e-323
[1] 0



-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
"Døden skal tape!" --- Nordahl Grieg



More information about the R-help mailing list