Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 is that 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.

like image 825
Serial Avatar asked Jul 13 '13 08:07

Serial


People also ask

Is NaN and infinity same?

Floating point. In floating-point calculations, NaN is not the same as infinity, although both are typically handled as special cases in floating-point representations of real numbers as well as in floating-point operations.

What does INF mean in coding?

Inf means infinity, and is the result of dividing a positive number by zero -- e.g., 1/0 → Inf. or computing a number larger than 1.796E308 (the largest number that your computer can represent in 64 bits) -- e.g. 1E307 * 100 → Inf.

Is NaN greater than infinity?

In comparison operations, positive infinity is larger than all values except itself and NaN, and negative infinity is smaller than all values except itself and NaN. NaN is unordered: it is not equal to, greater than, or less than anything, including itself.

What INF mean in Python?

inf constant returns a floating-point positive infinity. For negative infinity, use -math. inf .


2 Answers

nan means "not a number", a float value that you get if you perform a calculation whose result can't be expressed as a number. Any calculations you perform with NaN will also result in NaN.

inf means infinity.

For example:

>>> 2*float("inf") inf >>> -2*float("inf") -inf >>> float("inf")-float("inf") nan 
like image 36
Tim Pietzcker Avatar answered Oct 04 '22 06:10

Tim Pietzcker


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)

like image 61
Volatility Avatar answered Oct 04 '22 06:10

Volatility