[R] Single precision data behaviour with readBin()

Eric Thompson eric.thompson at tufts.edu
Thu Nov 9 22:20:44 CET 2006


Hi all,

I am running R version 2.4.0 (2006-10-03) on an i686 pc with Mandrake
10.2 Linux. I was given a binary data file containing single precision
numbers that I would like to read into R. In a previous posting,
someone suggested reading in such data as double(), which is what I've
tried:

> zz <- file(file, "rb")
> h1 <- readBin(con = zz, what = double(), n = 1, size = 4)
> h1
[1] 0.0500000007451

Except that I know that the first value should be exactly 0.05. To get
rid of the unwanted (or really unknown) values, I try using signif(),
which gives me:

> h1 <- signif(h1, digits = 8)
> h1
[1] 0.050000001

I suppose I could use:

> h1 <- signif(h1, digits = 7)
> h1
[1] 0.05

But this does not seem ideal to me. Apparently I don't understand
machine precision very well, because I don't understand where the
extra values are coming from. So I also don't know if this use of
signif() will be reliable for all possible values. What about a value
of 1.2e-8? Will this be read in as:

> signif(1.200000000034e-8, digits = 7)
[1] 1.2e-08

or could this occur?:

> signif(1.2000034e-8, digits = 7)
[1] 1.200003e-08

Thanks for any advice.

Eric Thompson
Graduate Student
Tufts University
Civil & Environmental Engineering
Medford, MA  02144



More information about the R-help mailing list