pythonpython-2.7naninfinity

What is inf and nan?


Just a question that I'm kind of confused about.

So I was messing around with float('inf') and kind of wondering what it is used for.

Also I noticed that if I add -inf + inf, I get nan that is the same as Zero or not.

I'm confused about what the uses of these two values are.

Also when I do nan - inf, I don't get -inf. I get nan. I'm sure it's all pretty simple but I stumbled upon them and didn't know what they do.


Solution

  • inf is infinity - a value that is greater than any other value. -inf is therefore smaller than any other value.

    nan stands for Not A Number, and this is not equal to 0.

    Although positive and negative infinity can be said to be symmetric about 0, the same can be said for any value n, meaning that the result of adding the two yields nan. This idea is discussed in this math.se question.

    Because nan is (literally) not a number, you can't do arithmetic with it, so the result of the second operation is also not a number (nan)