There have been some really great responses to this question. I would like to make a minor contribution by throwing the 'gmp' package into the mix.

On 1/7/2010 8:12 AM, Duncan Murdoch wrote:
On 07/01/2010 7:31 AM, Ulrich Keller wrote:
as.integer(.57 * 100)
[1] 56

Yes, as the man page states, non-integer values are truncated towards
zero.

 > .57*100 < 57
[1] TRUE

gmp provides the bigz and bigq classes which provide arbitrary precision integers and rationals, and would solve Ulrich's original
problem quite effectively:

> library(gmp)
>
> # bigq will not parse "0.57" but writing such a parser
> # would be a trivial extension.
> x = as.bigq("57")/100
> x
[1] "57/100"
>
> # as.integer(x*100) does not seem to be overloaded correctly, so
> # we need to use as.numeric first, but the end result is 57
> as.integer(as.numeric(x*100))
[1] 57
>
> # And we conclude by making some comparisons, and finding the that
> # the results are what a fifth-grader would expect, rather than
> # what a CS grad would expect :-)
> x*100<57
[1] FALSE
> x*100==57
[1] TRUE

Of course there is still the problem that:
> 1+1 == sqrt(2)*sqrt(2)
[1] FALSE
and gmp will not solve this . I don't know if there is an R-package for arbitrary-precision reals floating around, but probably not.
However, Wolfram Alpha will return the correct answer:
http://tr.im/1plus1equals2


Best,
Magnus

______________________________________________
R-help@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.

Reply via email to