Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do these three special floating-point values mean: positive infinity, negative infinity, NaN?

How can we use them in our codes, and what will cause NaN(not a number)?

like image 937
Johanna Avatar asked Jun 17 '09 14:06

Johanna


People also ask

What is NaN in floating-point?

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.

What is the difference between NaN and infinity?

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.

What is infinity in floating-point?

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.

What is positive and negative infinity?

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.


2 Answers

  • Positive infinity means going to infinity in the positive direction -- going into values that are larger and larger in magnitude in the positive direction.
  • Negative infinity means going to infinity in the negative direction -- going into values that are larger and larger in magnitude in the negative direction.
  • Not-a-number (NaN) is something that is undefined, such as the result of 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
like image 133
coobird Avatar answered Oct 25 '22 06:10

coobird


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.

like image 41
Cameron Avatar answered Oct 25 '22 06:10

Cameron