G'day Erik,
On Fri, 16 May 2008 10:45:43 -0500
[...]
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
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()
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?
> >> 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."
Thus, I would expect
Cheers,
Berwin
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.
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.