I'm writing a program in which I need to check if a certain "double" number is an exact integral square root or not. For example, if applied in the Pythagorean triplet problem, then for a=1, b=1, c is sqrt(2) which is not integral, hence will return false, whereas for a=3, b=4, c is exactly 5 and would return true..
So my current algorithm is I accept the sqrt of the number, type-cast into int in another variable, and then find the difference. Now I want to check whether the difference is exactly 0 or not, coz I've heard that using 0 as equality against a double is risky. Even 0.0000 or something won't do. So how exactly can I achieve this? Or can someone suggest a better algorithm for this task? (I desire speed efficiency, not memory..)
Here's my current function, for reference.. (Please don't mind, its in Java, but I guess its very basic, so people won't have any issues as such)
1 2 3 4 5 6 7 8 9
|
public static boolean isSquare(double a)
{
boolean isSquare=false;
if ((double)(a-(int)a)==0.0000000000)
isSquare=true;
return isSquare;
}
| |
Thank you in advance.. :)