How can we use them in our codes, and what will cause NaN(not a number)?
NaN: A floating point value meaning “Not a Number”. Support for NaN is provided for interoperability with other devices or systems that produce NaN values. Positive Infinity or Negative Infinity: Floating point value, sometimes represented as Infinity, –Infinity, INF, or –INF.
In comparison operations, positive infinity is larger than all values except itself and NaN, and negative infinity is smaller than all values except itself and NaN. NaN is unordered: it is not equal to, greater than, or less than anything, including itself. x == x is false if the value of x is NaN.
Infinity is represented by the largest biased exponent allowed by the format and a mantissa of zero. NaNs. A NaN (Not-a-Number) is a symbolic entity encoded in floating-point format.
Numbers approach negative infinity as they move left on a number line and positive infinity as they move right on a number line. Negative infinity is always less than any number, and positive infinity is greater than any number.
0/0
.And the constants from the specification of the Float
class:
Float.NEGATIVE_INFINITY
Float.POSITIVE_INFINITY
Float.NaN
More information can be found in the IEEE-754 page in Wikipedia.
Here's a little program to illustrate the three constants:
System.out.println(0f / 0f);
System.out.println(1f / 0f);
System.out.println(-1f / 0f);
Output:
NaN
Infinity
-Infinity
This may be a good reference if you want to learn more about floating point numbers in Java.
Positive Infinity is a positive number so large that it can't be represented normally. Negative Infinity is a negative number so large that it cannot be represented normally. NaN means "Not a Number" and results from a mathematical operation that doesn't yield a number- like dividing 0 by 0.
In Java, the Double and Float classes both have constants to represent all three cases. They are POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN.
Plus consider this:
double a = Math.pow(10, 600) - Math.pow(10, 600); //==NaN
Mathematically, everybody can see it is 0. But for the machine, it is an "Infinity" - "Infinity" (of same Rank), which is indeed NaN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With