Re: [R] Integer / floating point question

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Sat, 17 May 2008 01:04:33 +0800

G'day Erik,

On Fri, 16 May 2008 10:45:43 -0500
Erik Iverson <iverson_at_biostat.wisc.edu> wrote:

[...]
> The help page for '%%' addresses this a bit, but then caveats it with
> 'up to rounding error', which is really my question. Is there ever
> 'rounding error' with 2.0 %% 1 as opposed to 2 %% 1?

I am not in the position to give an authoritative answer, but I think there should be no problem with rounding error in the situation that you describe. At least I hope there is no problem, otherwise I would consider this a serious issue. :)

> >> However, my question is related to R FAQ 7.31, "Why doesn't R
> >> think these numbers are equal?" The first sentence of that FAQ
> >> reads, "The only numbers that can be represented exactly in R's
> >> numeric type are integers and fractions whose denominator is a
> >> power of 2."

Again, I did not write this FAQ answer and cannot give an authoritative answer, but the word "integer" in that answer does not IMHO refer to variables in R that are of integer type; in particular since the answer discusses what kind of numbers "can be represented exactly in R's numeric type". (Perhaps this should actually be plural since there are several numeric types?)

My interpretation is that 2.0 and 2 are both *text constants* that represent the integer 2, and that number is representable in a floating point (and in an integer).

The paper by Goldberg, referenced in FAQ 7.31, contains a discussion on whether it is possible (it is) to convert a floating point number from binary representation to decimal representation and then back; ending up with the same binary representation. This kind of questions are important if you use commands like write.table() or write.csv() which write out floating points in decimal representation, readable to normal humans. When you read the data back in, you want to end up with the exact same binary representation of the numbers. Goldberg is indeed an interesting paper to read.

And the comments I made above are based on my understanding of Goldberg, 2 and 2.0 are both decimal representation of the integer 2, and this number has an exact representation (in integer type variables and in floating point type variables). Hence, both these decimal representation should lead to a binary representation that correspond to that number exactly.

Thus, I would expect
R> x <- 2.0
R> x %% 1 == 0
always to work and to return TRUE. It is things like: R> x <- sqrt(2.0)^2
R> x %% 1 == 0
that FAQ 7.31 is about and what, IMHO, the comment in the help page of %% warns about; if the variable x contains a value that was created by some finite precision floating point calculations. But the conversion from a textual representation of an integer to a binary representation should not create problems.

Cheers,

        Berwin


R-help_at_r-project.org 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. Received on Fri 16 May 2008 - 17:58:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 16 May 2008 - 20:30:55 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive