[Rd] RFC: is.whole() ? {"how to judge if a variable is an integer"}
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Oct 28 11:48:07 CET 2014
On 28/10/2014, 5:32 AM, Martin Maechler wrote:
> Diverted to R-devel, as I'm requesting comments about a proposal
> to add is.whole() to R just so this issue does not trail on for
> centuries (;-), see below.
>
I didn't read the thread in question, but I think Bill's comment is
crucial: what properties of integers are you interested in testing?
One that comes up a lot is that some non-integral values print as
integers, and people get confused when using them as indices. For
example, this prints results that surprise some people:
x <- 1:10
index <- 4 - 1.e-10
index
x[index]
I would think is.whole(index) should return FALSE here, but the sfsmisc
function defaults to a nonzero tolerance, and returns TRUE.
Duncan Murdoch
>>>>>> William Dunlap <wdunlap at tibco.com>
>>>>>> on Sat, 18 Oct 2014 10:33:05 -0700 writes:
>
> > 3. all.equal(a, as.integer(a))
> > Note that this one tests if 'a' can be stored accurately as a 32-bit signed
> > integer. If you want to know if 'a' can be used as an accurate count, then
> > you want to test if a+1>a (use abs() in case a is negative). E.g., try this
> > for a<-2^49-1, about 5*10^14.
>
> > You have to decide what properties of integers you are interested in.
>
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
>
> good point, thank you, Bill.
>
> This whole issue comes up about once year (at least), it seems to me,
> and every time there are some good and some not so good propositions,
> some aiming for vectorized / whole object solutions some, only
> assuming scalar input.
>
> Indeed, I'd claim that all three proposals cited below are not
> good enough in one way or the other.... though one
> could argue that a "tolerance = 0" version would be good enough,
> and hence the
> function(x) x %% 1 == 0
> would be sufficient.
>
> In the CRAN package 'sfsmisc' (which I maintain), Alain Hauser
> recently added an
>
> is.whole()
>
> function which works "vectorized" and uses a 'tolerance' and
> then all.equal(), but in better way than (most / all ?) what was proposed in
> this thread (line 486 ff of
> https://github.com/mmaechler/sfsmisc/blob/master/R/misc-goodies.R ).
>
> Further, the CRAN packages
> 'gmp' ("infinite" precision integer and rational arithmetic) and
> 'Rmpfr' ('infinite' precision floating point arithmetic) contain
> an S3 generic is.whole() function and methods for their own
> number classes and a default method (( which "however" assumes
> a tolerance of zero, the same as the x %% 1 == 0 proposal)).
>
> Given all this, the r-help and stackoverflow threads,
> maybe we should decide that such an is.whole() function should
> be added to R and maintained (by me for the time), so we do have
> a better place to point people to, and well documented -- and
> eventually "optimized" -- behavior ?
>
> Personally I do think I'd want a "signature" of
> function (x, tolerance = sqrt(.Machine$double.eps))
>
>
> Martin
>
>
> > On Sat, Oct 18, 2014 at 10:02 AM, PO SU <rhelpmaillist at 163.com> wrote:
> >>
> >> Tks for your help, after investigate in your link, i find there seems three ways can be adoped:
> >> 1. is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol)
> >> e.g. is.wholenumber(1)
> >> 2. x%%1==0
> >>
> >>
> >> 3. all.equal(a, as.integer(a))
> >>
> >>
> >> and also included your last suggestion using floor. and also tks for other helpers!
> >> --
> >>
> >> PO SU
> >> mail: desolator88 at 163.com
> >> Majored in Statistics from SJTU
> >>
> >> At 2014-10-18 22:48:15, "Sergio Fonda" <sergio.fonda99 at gmail.com> wrote:
> >>
> >> Sorry for my previous hurry misunderstanding.
> >> Try this link:
> >> http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer
> >>
> >> 2014-10-18 16:25 GMT+02:00 PO SU <rhelpmaillist at 163.com>:
> >>
> >> It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but deeply, when i want to know 1 is an integer, there seems no easy way to get the answer.
> >>
> >> So, is there anyone happen to know it?
> >>
>
> >>
> >> At 2014-10-18 20:10:09, "S Ellison" <S.Ellison at LGCGroup.com> wrote:
> >>
> >>>> But i use a<-10/b , b is some value ,may be 5, maybe 5.5
> >>
> >>> If you do floating point arithmetic on integers you'll usually get floating point answers, including the 5.0.
> >>
> >>>
> >>
> >>> See FAQ 7.31 for the usual floating point problem, and ?all.equal for the usual answer to it. You could see if a result is close to an integer by,for example, using all.equal to compare it to itself after rounding.
> >>
> >>>
> >>
> >>> S
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list