[R] Single precision data behaviour with readBin()

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 9 22:31:35 CET 2006


On 11/9/2006 4:20 PM, Eric Thompson wrote:
> 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. 

That's impossible.  You can't represent 0.05 in either single or double 
precision floats.  What you're seeing is the error in the single 
precision version of its representation.

Duncan Murdoch


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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list