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

后端 未结 6 625
无人及你
无人及你 2020-12-31 12:22

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

6条回答
  •  感情败类
    2020-12-31 12:51

    • 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
    

提交回复
热议问题