[R] Change of sign with division by zero, i.e. -1/0 = Inf ??
William Dunlap
wdunlap at tibco.com
Mon Mar 10 20:55:12 CET 2014
The double precision number system (in the IEEE 754 format) includes
two zeroes, one of each sign. They are hard to tell apart, but their reciprocals
are Inf's of the appropriate sign and, in R 3.0.2, sprintf("%g",...) distinguishes them.
> z <- c(0.0, -0.0)
> z
[1] 0 0
> z[1] == z[2]
[1] TRUE
> identical(z[1], z[2])
[1] TRUE
>
> 1/z
[1] Inf -Inf
> sprintf("%g", z)
[1] "0" "-0"
Bill Dunlap
TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Cesar Caballero
> Sent: Monday, March 10, 2014 8:27 AM
> To: r-help at r-project.org
> Subject: [R] Change of sign with division by zero, i.e. -1/0 = Inf ??
>
> 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.
>
> 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