Being pedantic < 0will not give you all the negative numbers.
double d = -0.0;
System.out.println(d + " compared with 0.0 is " + Double.compare(d, 0.0));
System.out.println(d + " < 0.0 is " + (d < 0.0));
prints
-0.0 compared with 0.0 is -1
-0.0 < 0.0 is false
-0.0 negative but not less 0.0
you can use
public static boolean isNegative(double d) {
return Double.compare(d, 0.0) < 0;
}
A more efficient, if dumber, version is to check the signed bit.
public static boolean isNegative(double d) {
return Double.doubleToRawLongBits(d) < 0;
}
Note. In IEEE-754, a NaN may have the same signed bit as a negative number.
source
share