Search code examples
javafloating-pointnaninfinity

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


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


Solution

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