Cesar Caballero
2014-Mar-10 15:26 UTC
[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
Sarah Goslee
2014-Mar-10 19:30 UTC
[R] Change of sign with division by zero, i.e. -1/0 = Inf ??
Hi, Without a reproducible example (providing data with dput(), please) it's hard to say for certain, but my suspicion is that this is a R FAQ 7.31 issue. Sarah On Mon, Mar 10, 2014 at 11:26 AM, 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. > > 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 >-- Sarah Goslee http://www.functionaldiversity.org
Berend Hasselman
2014-Mar-10 19:43 UTC
[R] Change of sign with division by zero, i.e. -1/0 = Inf ??
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.
William Dunlap
2014-Mar-10 19:55 UTC
[R] Change of sign with division by zero, i.e. -1/0 = Inf ??
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.