[R] Change of sign with division by zero, i.e. -1/0 = Inf ??

Berend Hasselman bhh at xs4all.nl
Mon Mar 10 20:43:01 CET 2014


On 10-03-2014, at 16:26, Cesar Caballero <c.caballero at bcbl.eu> wrote:

> Hi R-users,
> 
> I am facing a strange behaviour that changes the sign of Inf with a division by zero.
> 
> In my script, I have defined the following 3 variables and the elementwise division:
> 
>> lambda_k[gamma_lambda]  # numeric vector
> [1] -1  0
>> del_lambda[gamma_lambda] # numeric vector
> [1]  0 -1
>> gamma_lambda  # integer vector with the indexes of the elements in lambda_k & del_lambda
> [1] 74 11
>> lambda_k[gamma_lambda] / del_lambda[gamma_lambda]
> [1] Inf   0
> 
> The result is surprising instead of -Inf 0 which I would expect because the elementwise division of the elements with index 74 is -1/0.
> 
> 
> Surprisingly, if I try to do the division with just newly defined numeric vectors with the same values, I obtain the correct result, i.e.
>> c(-1,0) / c(0,-1)
> [1] -Inf    0 
> 
> 
> How can this be explained? Any input would be really welcomed.
> 

Most likely the 0 you show for del_lambda is not exactly 0.
Look at this

> c(-0.000000000000000,-1)
[1]  0 -1

> c(-1,0) / c(-0.000000000000000,-1)
[1] Inf   0

Berend


> For your information, I'm using R version 3.0.2 (2013-09-25) and Rstudio Version 0.98.501 on Platform: x86_64-apple-darwin10.8.0 (64-bit).
> 
> 
> Thanks very much for your help,
> 
> Cesar
> 
> ----------------------------------------------------------------------
> Cesar Caballero
> www.bcbl.eu 
> 
> Legal disclaimer/Aviso legal/Lege-oharra: www.bcbl.eu/legal-disclaimer
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list