[R] How to handle large numbers?
tlumley at u.washington.edu
Wed Feb 11 12:05:20 CET 2009
On Wed, 11 Feb 2009, Feng Li wrote:
> 1, Why both R and Matlab give 0*Inf==NaN? To my knowledge, it should be zero
> mathematically. Am I right?
No. 0*Inf is NaN according to the floating point arithmetic standards that R depends on.
It's true that 0*x==0 for all finite x, but it's also true that x*Inf==Inf for all non-zero x, and you can't preserve both of these with 0*Inf.
> 2, I need to calculate e.g. exp(a)/(exp(b)+c), where both a and b are very
> large numbers (>>1000, e.g a=1000, b=1007, and c=5). R gives me NaN when I
> use the following command:
>  NaN
> I am pretty sure this should be close to zero.
No. It should be close to 1. Try 1000/(1000+5) for a simpler example.
> My question is whether there
> is a general way to solve this kind of question or should I do some settings
> before computing?
exp(1000)/(exp(1007)+5) is 1/(exp(7)+5*exp(-1000)), which is the same as 1/exp(7) to more than 400 digits accuracy.
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
More information about the R-help