[R-SIG-Mac] Inaccurate inaccuracies on Mac only?

Hans W Borchers hwborchers at gmail.com
Sat Nov 30 14:54:47 CET 2013


Dear R Mac colleagues,

when I am running the following piece of code on Ubuntu Linux 12.04 LTS or on
Windows 7 -- with R 3.0.2 --, I get the results as indicated:

    set.seed(8237)
    x <- runif(32, -1, 1)
    y <- runif(32, -1, 1)
    u <- cbind(x[1], y[1])

    u1 <- u %*% t(u)
    u2 <- apply(u * u, 1, sum)
    sqrt(u2 + u2 - 2*u1)
    ##      [,1]
    ## [1,]  NaN
    ## Warning message:
    ##    In sqrt(u2 + u2 - 2 * u1) : NaNs produced
    u2 + u2 - 2*u1
    ##               [,1]
    ## [1,] -2.220446e-16

Theoretically, the last value should be zero. Of course, I am *not* surprised
to see this is not the case under finite precision arithmetics.

But what did surprise me was that under Mac OS X 10.6 and R 3.0.2 I get the
following, exact results:

    set.seed(8237)
    x <- runif(32, -1, 1)
    y <- runif(32, -1, 1)
    u <- cbind(x[1], y[1])

    u1 <- u %*% t(u)
    u2 <- apply(u * u, 1, sum)
    sqrt(u2 + u2 - 2*u1)
    ##      [,1]
    ## [1,]    0
    u2 + u2 - 2*u1
    ##      [,1]
    ## [1,]    0

I always thought that all these systems and versions comply to the IEEE
floating point standard and return the same (accurate or inaccurate) answers.

The problem here is: When I test a package on Mac, I cannot be sure it will
run without errors on other systems. Actully, I found this when running some
examples from help pages, written on the Mac.

Was I wrong, or is there something special about the Mac version of R ?

Many thanks
Hans Werner



More information about the R-SIG-Mac mailing list