How can we use them in our codes, and what will cause NaN(not a number)?
6 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:
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

- 159,216
- 35
- 211
- 226
-
but I think 1/0 will cause positive infinity :-? – Johanna Jun 17 '09 at 15:07
-
Yes, you're correct -- that was my error, and it has been fixed. – coobird Jun 17 '09 at 15:24
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.
-
I tried to fix the broken link but it got rejected. [Here](http://introcs.cs.princeton.edu/java/91float/) is a really good resource that also includes a QA section with links for further exploration. – Michael Hogenson Jul 12 '13 at 18:46
-
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). – Peter Webb Aug 14 '13 at 09:17
-
@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. – supercat Apr 16 '14 at 19:44
-
@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. – Ruslan Feb 02 '17 at 07:33
- 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.

- 1,259
- 3
- 14
- 27
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.

- 78,318
- 8
- 63
- 70
You can use them as any other number:
e.g:
float min = Float.NEGATIVE_INFINITY;
float max = Float.POSITIVE_INFINITY;
float nan = Float.NaN;

- 182
- 9
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.
this is not a complete answer(or not clarified enough) - 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 order) witch is indeed NaN...

- 159
- 1
- 9
-
1If quoting an answer, it's usually better left as a comment or suggesting an edit. Answers should be able to stand alone. – chrisbajorin Mar 30 '16 at 00:42
-