Alex Rivera | Logout

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

Asked 2009-06-17T14:58:12.930
13

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

Edit
Report

3 Answers

17
  • 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
answered 2009-06-17T15:01:30.303
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.

answered 2009-06-17T15:06:13.833
0

You can use them as any other number:

e.g:

float min = Float.NEGATIVE_INFINITY;
float max = Float.POSITIVE_INFINITY;
float nan = Float.NaN;
answered 2009-06-17T15:11:29.610

Your Answer