I am trying to calculate coordinate transformations and in the process of debugging my code using debug I found the following Browse[1]> direction[i] [1] -1.570796 Browse[1]> cos(direction[i]) [1] 6.123032e-17 Browse[1]> cos(-1.570796) [1] 3.267949e-07 Browse[1]> direction[i] [1] -1.570796 Browse[1]> cos(direction[i]) [1] 6.123032e-17 Browse[1]> cos(-1.570796) [1] 3.267949e-07 Browse[1]> x<-direction[i] Browse[1]> x [1] -1.570796 Browse[1]> cos(x) [1] 6.123032e-17 I am not sure why I am getting one values when I am using a variable that stores the value and another when I use the value directly. Am I missing something here? Can someone comment? Thanks ../Murli
Nair, Murlidharan T wrote:> > I am trying to calculate coordinate transformations and in the process of > debugging my code using debug I found the following > > Browse[1]> direction[i] > [1] -1.570796 > Browse[1]> cos(direction[i]) > [1] 6.123032e-17 > Browse[1]> cos(-1.570796) > [1] 3.267949e-07 > Browse[1]> direction[i] > [1] -1.570796 > Browse[1]> cos(direction[i]) > [1] 6.123032e-17 > Browse[1]> cos(-1.570796) > [1] 3.267949e-07 > Browse[1]> x<-direction[i] > Browse[1]> x > [1] -1.570796 > Browse[1]> cos(x) > [1] 6.123032e-17 > > I am not sure why I am getting one values when I am using a variable that > stores the value and another when I use the value directly. Am I missing > something here? > >Because you are not looking at the full precision of the data. Try print(x,digits=20) ... Ben Bolker -- View this message in context: http://www.nabble.com/Trig-functions-strange-results-tp24484518p24485352.html Sent from the R help mailing list archive at Nabble.com.
FAQ 7.31 This is what happens with floating point number and you are only printing out 7 digits of precision; look at the results> cos(-1.570796)[1] 0.0000003267949> cos(-1.5707961) # just incrementing the digit that was not displayed[1] 0.0000002267949> cos(-1.5707962)[1] 0.0000001267949> cos(-1.5707963)[1] 0.00000002679490> cos(-1.5707964) # notice the sign change[1] -0.0000000732051>On Tue, Jul 14, 2009 at 1:45 PM, Nair, Murlidharan T<mnair at iusb.edu> wrote:> I am trying to calculate coordinate transformations and in the process of debugging my code using debug I found the following > > Browse[1]> direction[i] > [1] -1.570796 > Browse[1]> cos(direction[i]) > [1] 6.123032e-17 > Browse[1]> cos(-1.570796) > [1] 3.267949e-07 > Browse[1]> direction[i] > [1] -1.570796 > Browse[1]> cos(direction[i]) > [1] 6.123032e-17 > Browse[1]> cos(-1.570796) > [1] 3.267949e-07 > Browse[1]> x<-direction[i] > Browse[1]> x > [1] -1.570796 > Browse[1]> cos(x) > [1] 6.123032e-17 > > I am not sure why I am getting one values when I am using a variable that stores the value and another when I use the value directly. ?Am I missing something here? > > Can someone comment? > > Thanks ../Murli > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
I had some doubts that it may be due to precision. The value stored in direction[i] is (0-90)*pi/180 How can I specify the precision to be used in computation to R? Thanks ../Murli -----Original Message----- From: jim holtman [mailto:jholtman at gmail.com] Sent: Tuesday, July 14, 2009 2:44 PM To: Nair, Murlidharan T Cc: r-help at r-project.org Subject: Re: [R] Trig functions strange results FAQ 7.31 This is what happens with floating point number and you are only printing out 7 digits of precision; look at the results> cos(-1.570796)[1] 0.0000003267949> cos(-1.5707961) # just incrementing the digit that was not displayed[1] 0.0000002267949> cos(-1.5707962)[1] 0.0000001267949> cos(-1.5707963)[1] 0.00000002679490> cos(-1.5707964) # notice the sign change[1] -0.0000000732051>On Tue, Jul 14, 2009 at 1:45 PM, Nair, Murlidharan T<mnair at iusb.edu> wrote:> I am trying to calculate coordinate transformations and in the process of debugging my code using debug I found the following > > Browse[1]> direction[i] > [1] -1.570796 > Browse[1]> cos(direction[i]) > [1] 6.123032e-17 > Browse[1]> cos(-1.570796) > [1] 3.267949e-07 > Browse[1]> direction[i] > [1] -1.570796 > Browse[1]> cos(direction[i]) > [1] 6.123032e-17 > Browse[1]> cos(-1.570796) > [1] 3.267949e-07 > Browse[1]> x<-direction[i] > Browse[1]> x > [1] -1.570796 > Browse[1]> cos(x) > [1] 6.123032e-17 > > I am not sure why I am getting one values when I am using a variable that stores the value and another when I use the value directly. ?Am I missing something here? > > Can someone comment? > > Thanks ../Murli > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Tue, Jul 14, 2009 at 1:45 PM, Nair, Murlidharan T <mnair@iusb.edu> wrote:> I am trying to calculate coordinate transformations and in the process of > debugging my code using debug I found the following > > Browse[1]> direction[i] > [1] -1.570796 > Browse[1]> cos(direction[i]) > [1] 6.123032e-17 > Browse[1]> cos(-1.570796) > [1] 3.267949e-07 > ... > I am not sure why I am getting one values when I am using a variable that > stores the value and another when I use the value directly. Am I missing > something here? >Because you are not using the same value. You say in a later message that your variable direction[i] was set to (0-90)*pi/180. So let's look at that:> x <- (0-90)*pi/180 > x - (-1.570796)[1] -3.267949e-07 That is, (0-90)*pi/180 is not exactly equal to -1.570796, but rather to -1.570796326794897:> print(x,digits=16)[1] -1.570796326794897 And that is equal to the calculated value. Well, almost:> print(x,digits=17)[1] -1.570796326794897 << the most digits R will print for a float> -1.570796326794897 - x[1] -4.440892e-16 << a very tiny difference See the R FAQ: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f By the way, there is a bug in the R print routine which does not print out the full precision even if you specify it....> -1.5707963267948965 - x << one more digit is actually needed[1] 0 [[alternative HTML version deleted]]