Eine weitere Lösung ist ein bisschen arithmetics mit:
is.natural2 <- function(x,tol=.Machine$double.eps^0.5){
sign(x) - abs(x-round(x)) >= 1-tol
}
Wenn nun alle Lösungen überprüft, es stellte sich heraus, dass das eine von Tal und der von Karakal nicht das richtige Ergebnis gegeben hat:
is.natural <- function(x)
{
x>0 & identical(round(x), x)
}
is.natural2 <- function(x,tol=.Machine$double.eps^0.5){
sign(x) - abs(x-round(x)) >= 1-tol
}
is.natural3 <- function(x, dig=15){ x > 0 & zapsmall(x, dig) == round(x) }
is.natural4 <- function(x,tol=.Machine$double.eps^0.5){
x > 0 &
isTRUE(all.equal(x,round(x),
tolerance=tol,
check.attributes=FALSE,
check.names=FALSE))
}
is.naturalnumber <- function(x, tol = .Machine$double.eps^0.5){
x > 0 & abs(x - round(x)) < tol
}
Dann:
> X <- (seq(0,3,by=0.5)^0.5)^2
> is.natural(X)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE # wrong
> is.natural2(X)
[1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE
> is.natural3(X)
[1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE
> is.natural4(X)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE # wrong
> is.naturalnumber(X)
[1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE
bezüglich Zeitablauf:
> X <- (seq(0,1000000,by=0.5)^0.5)^2
> system.time(is.natural2(X))
user system elapsed
0.24 0.03 0.26
> system.time(is.natural3(X))
user system elapsed
0.67 0.00 0.67
> system.time(is.naturalnumber(X))
user system elapsed
0.22 0.01 0.23
was macht Marcog zum Gewinner.
Ich bin nicht vertraut mit R aber ein bisschen Verschiebung wäre der schnellste. –
Wie wäre es damit? http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer –