对于Double和Float这种浮点型来说,存在无穷大(POSTIVE_INFINITY)和无穷小(NAGATIVE_INFINITY),NaN的概念。
注意:NaN在任何时候都不会相等。
public class Test {
public static void main(String[] args) {
System.out.println(Double.NEGATIVE_INFINITY==Float.NEGATIVE_INFINITY); // TRUE
System.out.println(Double.NEGATIVE_INFINITY==Double.NEGATIVE_INFINITY); // TRUE
System.out.println(Float.NEGATIVE_INFINITY==Float.NEGATIVE_INFINITY); // TRUE
System.out.println(Float.NaN==Float.NaN); // FALSE
System.out.println(Double.NaN==Double.NaN); // FALSE
}
}
那么什么时候会出现这种情况呢?下图说明:
如何去判断一个数是不是infinity或者NaN呢?
1.isInifinite() 用于判断一个数是不是infinity(无穷大,包括正无穷和负无穷);
1 /**
2 * Returns {@code true} if the specified number is infinitely
3 * large in magnitude, {@code false} otherwise.
4 *
5 * @param v the value to be tested.
6 * @return {@code true} if the value of the argument is positive
7 * infinity or negative infinity; {@code false} otherwise.
8 */
9 public static boolean isInfinite(double v) {
10 return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
11 }
2.isNaN() 用于判断一个数是否是一个数;
1 /**
2 * Returns {@code true} if the specified number is a
3 * Not-a-Number (NaN) value, {@code false} otherwise.
4 *
5 * @param v the value to be tested.
6 * @return {@code true} if the value of the argument is NaN;
7 * {@code false} otherwise.
8 */
9 public static boolean isNaN(double v) {
10 return (v != v);
11 }