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

后端 未结 6 642
无人及你
无人及你 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:36

    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.

提交回复
热议问题