Cypress
Cypress

Reputation: 357

R: is.integer doesn't work on square root

Basically, I want to check if the square root of a number is an integer, so I tried the following function:

is.integer(sqrt(4))

The expected value is TRUE whereas the actual result is FALSE. I have read some other posts and it seems I need to use L to force into an integer. However, not sure how should I make it work in my case.

Upvotes: 3

Views: 349

Answers (1)

Erwin Kalvelagen
Erwin Kalvelagen

Reputation: 16797

Yes. Even:

is.integer(1)
## [1] FALSE

because the type (opposed to the value) is not integer. Look at the help ?is.integer. A function is.wholenumber is shown there:

is.wholenumber <-
     function(x, tol = .Machine$double.eps^0.5)  
         abs(x - round(x)) < tol
is.wholenumber(sqrt(4))
## [1] TRUE

Upvotes: 7

Related Questions