0

So here is a question I have mostly out of curiosity. I'm learning Pi with Paul Teetor's R Cookbook. I was playing around with some of the commands, when I came across this oddity:

> v
[1] 3.000000 3.140000 4.000000 3.141593 3.141593 3.141593 3.141593
> pi == v
[1] FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE

So checking what R considers pi to be,

> pi
[1] 3.141593

Apparently, R is giving me conflicting opinions on what pi is, or to show it more clearly I put the two into a matrix:

> v <- c(v, pi==v)
> mat <- matrix(v, 7, 2)
> mat
         [,1] [,2]
[1,] 3.000000    0
[2,] 3.140000    0
[3,] 4.000000    0
[4,] 3.141593    1
[5,] 3.141593    0
[6,] 3.141593    1
[7,] 3.141593    0

So apparently R considers pi to be 3.141593 on line 4, then not that on line 5, then back to 3.141593 on line 6, changing it's mind on 7 and so on. Does anyone know what is the cause of this interpreter's indecisiveness?

BrotherJack
  • 319
  • 1
  • 19
  • 2
    You probably aren't displaying enough digits of precision. Try displaying the *difference*, i.e. `(v - pi)` (or whatever the R syntax is for this). – Oliver Charlesworth Jul 02 '13 at 14:25
  • 1
    It probably has to do with http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal – GSee Jul 02 '13 at 14:30
  • Also, could it be that you hit [this](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) problem, just the other way around? Try `isTRUE(all.equal())`. – Christoph_J Jul 02 '13 at 14:31
  • @GSee Posting my thoughts 30 seconds earlier than me...How did you do that, man? I'm impressed ;-) – Christoph_J Jul 02 '13 at 14:32
  • Yeah, I'm pretty sure you guys are correct. I was assuming that the console output was what R saw pi as. This was a very bad assumption. The last four elements of v were entered as pi, 3.14159265359, (pi to 100 digits), 3.141593. It's essentially seeing pi, smaller than pi, pi, smaller than pi. – BrotherJack Jul 02 '13 at 14:37
  • For those that are interested it appears that R takes pi out to 15 digits as: 3.14159265358979 == pi is FALSE, but 3.141592653589793 == pi and beyond is TRUE. – BrotherJack Jul 02 '13 at 14:45
  • 1
    No, that's only true for the default builtin. You can easily extend it with your own calculation, or via `gmp` . More important, in the general case of comparing two floats, the answer is uncertain at the level of 2^(-16) or `.Machine.eps` . – Carl Witthoft Jul 02 '13 at 15:18
  • Maybe you want to check "1 Falling into the Floating Point Trap" on the R Inferno... http://www.burns-stat.com/documents/books/the-r-inferno/ – vodka Jul 02 '13 at 17:06

1 Answers1

3

I don't get the same behaviour,

test <- function(digits, replications=10)
  c(representation = as.character(signif(pi, digits)), 
    equality = any(replicate(replications, pi == signif(pi, digits))))

t(sapply(1:16, test))

      representation     equality
 [1,] "3"                "FALSE" 
 [2,] "3.1"              "FALSE" 
 [3,] "3.14"             "FALSE" 
 [4,] "3.142"            "FALSE" 
 [5,] "3.1416"           "FALSE" 
 [6,] "3.14159"          "FALSE" 
 [7,] "3.141593"         "FALSE" 
 [8,] "3.1415927"        "FALSE" 
 [9,] "3.14159265"       "FALSE" 
[10,] "3.141592654"      "FALSE" 
[11,] "3.1415926536"     "FALSE" 
[12,] "3.14159265359"    "FALSE" 
[13,] "3.14159265359"    "FALSE" 
[14,] "3.1415926535898"  "FALSE" 
[15,] "3.14159265358979" "FALSE" 
[16,] "3.14159265358979" "TRUE"  

only returns TRUE for 16 digits, which is understandable because of the finite precision of doubles.

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Yeah, I didn't repeat 'pi' those 4 times. I neglected to mention the important point which is that the last four elements of v are: pi, 3.14159265359, (pi to 100 digits), 3.141593. So that's why I'm getting that behavior since R takes pi out to 15 digits beyond the decimal. – BrotherJack Jul 02 '13 at 14:48