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]]


