[R] 0.1 + 0.2 != 0.3 revisited
Duncan Murdoch
dmurdoch at pair.com
Fri Feb 6 17:26:25 CET 2004
On Fri, 6 Feb 2004 12:55:05 -0000, "Simon Fear"
<Simon.Fear at synequanon.com> wrote :
>Prompted by Peter Dalgard's recent elegant "intbin" function,
>I have been playing with the extension to converting reals to binary
>representation. The decimal part can be done like this:
>
>decbase <- function(x, n=52, base=2) {
> if(n) {
> x <- x*base
> paste(trunc(x), decbase(x%%1, n-1, base), sep="")
> }
>}
>
>n=52 default because that's the number of bits in the significand of
>a 64-bit float.
Remember that IEEE double formats are complicated, they're not fixed
point formats.
Both 0.1 and 0.2 are less than 1, so the n=52 count is wrong. I think
0.1 would be stored as (1 + 0.6)*2^(-4) and 0.2 would be stored as (1
+ 0.6)*2^(-3), whereas 0.3 would be stored as (1 + 0.2)*2^(-2). You
should expect 56 decimal (binary?) place accuracy on 0.1, 55 place
accuracy on 0.2, and 54 place accuracy on 0.3. It's not surprising
weird things happen!
Duncan Murdoch
More information about the R-help
mailing list