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

22,696

Solution 1

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.

Solution 2

  • 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:

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

Solution 3

  • 1/0 will result in positive infinity.
  • 0/0 will result in Nan. You can use NaN as any other number, eg: NaN+NaN=NaN, NaN+2.0=NaN
  • -1/0 will result in negative infinity.

Infinity (in java) means that the result of an operation will be such an extremely large positive or negative number that it cannot be represented normally.

Solution 4

The idea is to represent special numbers which can arise naturally from operations on "normal" numbers. You could see infinity (both positive and negative) as "overflow" of the floating point representation, the idea being that in at least some conditions, having such a value returned by a function still gives meaningful result. They still have some ordering properties, for example (so they won't screw sorting operations, for example).

Nan is very particular: if x is Nan, x == x is false (that's actually one way to test for nan, at least in C, again). This can be quite confusing if you are not used to floating point peculiarities. Unless you do scientific computation, I would say that having Nan returned by an operation is a bug, at least in most cases that come to mind. Nan can come for various operations: 0/0, inf - inf, inf/inf, 0 * inf. Nan does not have any ordering property, either.

Share:
22,696

Related videos on Youtube

Johanna
Author by

Johanna

Updated on March 30, 2020

Comments

  • Johanna
    Johanna about 4 years

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

  • coobird
    coobird almost 15 years
    Yes, you're correct -- that was my error, and it has been fixed.
  • Michael Hogenson
    Michael Hogenson almost 11 years
    I tried to fix the broken link but it got rejected. Here is a really good resource that also includes a QA section with links for further exploration.
  • Peter Webb
    Peter Webb almost 11 years
    I disagree with the characterisation (here and elsewhere) that positive infinity is a "number so large it can't be represented normally". It can be a number so large it cannot be represented as a normal float, but in practice it usually derives from something like 5f/0f, which isn't a large (Real) number; it is a naïve version of infinity. (Technically +inf, -inf and NaN are extensions to the Reals which provide closure over any arithmetic operation, but at the cost of violating basic field axioms. This doesn't matter much to computer scientists, it does to mathematicians).
  • supercat
    supercat about 10 years
    @PeterWebb: The fact that NaN was unfortunately defined not only as unranked, but also as unequal to itself, means that float and double not only violate field axioms--they aren't even equivalence classes. That's of interest to many more people than just mathematicians, since it compels the use of ugly kludges to make collections work.
  • chrisbajorin
    chrisbajorin about 8 years
    If quoting an answer, it's usually better left as a comment or suggesting an edit. Answers should be able to stand alone.
  • Alex
    Alex about 8 years
    ok I did not know i could edit others posts... it like a git :)
  • Ruslan
    Ruslan over 7 years
    @PeterWebb in fact, even without ±inf/NaN float, double etc. violate field axioms, namely, associativity. In any case, they aren't supposed to be basic building blocks for mathematicians who want to work with true Reals — for those there are computer algebra systems which can work with exact numbers.