Home > Back-end >  How to determine if the square root of a number is integer?
How to determine if the square root of a number is integer?

Time:07-19

isinteger(sqrt(3))
0
isinteger(sqrt(4))
0

Both answers give zero. The answers must be:

isinteger(sqrt(3))
0
isinteger(sqrt(4))
1

CodePudding user response:

isinteger checks for type. integer is a type of variable, not a property of a number. e.g. isinteger(2.0) returns 0.

Try:

mod(sqrt(x),1) == 0

However, you may still have issues with this due to numerical precision.

CodePudding user response:

You may do as well

y = sqrt(4);
y==round(y)

or to take round-off error into account with a (2*eps) relative tolerance

abs(y-round(y)) <= 2*eps*y
  • Related