title: How are Inf and NaN implemented?
body: <p>As mathematical concepts, I am well aware of what <code>inf</code> and <code>nan</code> actually are. But what I am really interested in is how they are implemented in programming languages.</p>
  
  <p>In python, I can use <code>inf</code> and <code>nan</code> in arithmetic and conditional expressions, like this:</p>
  
  <pre><code>&gt;&gt;&gt; nan = float('nan')
  &gt;&gt;&gt; inf = float('inf')
  &gt;&gt;&gt; 1 + inf
  inf
  &gt;&gt;&gt; inf + inf
  inf
  &gt;&gt;&gt; inf - inf
  nan
  </code></pre>
  
  <p>This would lead me to believe that python internally has a special reserved bit sequence for these two mathematical quantities, and no other number can assume these positions. Is my assumption correct? Can you please enlighten me in this regard?</p>
  
  <p>If my assumption is correct, then this can be explained easily:</p>
  
  <pre><code>&gt;&gt;&gt; inf == inf
  True
  </code></pre>
  
  <p>However, this is not:</p>
  
  <pre><code>&gt;&gt;&gt; nan == nan
  False
  </code></pre>
  
  <p>Obviously, in mathematics, this is the right answer. But how does python know that it should spit out <code>False</code> in this instance? </p>
  
  <p>Furthermore, how does python's implementation differ from that of java or c++?</p>