title: How to add to/subtract from float value the smallest possible value in pure python [explained how is different]?
body: <p>This question is <strong>only for Python programmers</strong>. This question is <strong>not duplicate not working</strong> <a href="https://stackoverflow.com/questions/6063755/increment-a-python-floating-point-value-by-the-smallest-possible-amount">Increment a python floating point value by the smallest possible amount</a> see explanation bottom.</p>
  
  <hr>
  
  <p>I want to add/subtract for any float some smallest values which will change this float value about one bit of <a href="http://en.wikipedia.org/wiki/Floating_point" rel="nofollow noreferrer">mantissa/significant part</a>. How to calculate such small number efficiently in <strong>pure Python</strong>.</p>
  
  <p>For example I have such array of x:</p>
  
  <pre><code>xs = [1e300, 1e0, 1e-300]
  </code></pre>
  
  <p>What will be function for it to generate <strong>the smallest value</strong>? All assertion should be valid.</p>
  
  <pre><code>for x in xs:
    assert x &lt; x + smallestChange(x)
    assert x &gt; x - smallestChange(x)
  </code></pre>
  
  <p>Consider that <code>1e308 + 1 == 1e308</code> since <code>1</code> does means <code>0</code> for mantissa so `smallestChange' should be dynamic. </p>
  
  <p>Pure Python solution will be the best.</p>
  
  <hr>
  
  <p><strong>Why this is not duplicate of <a href="https://stackoverflow.com/questions/6063755/increment-a-python-floating-point-value-by-the-smallest-possible-amount">Increment a python floating point value by the smallest possible amount</a></strong> - two simple tests prove it with invalid results.</p>
  
  <p>(1) The question is not aswered in <a href="https://stackoverflow.com/questions/6063755/increment-a-python-floating-point-value-by-the-smallest-possible-amount">Increment a python floating point value by the smallest possible amount</a> difference:</p>
  
  <p><strong><a href="https://stackoverflow.com/questions/6063755/increment-a-python-floating-point-value-by-the-smallest-possible-amount">Increment a python floating point value by the smallest possible amount</a> just not works try this code</strong>:</p>
  
  <pre><code>import math
  epsilon  = math.ldexp(1.0, -53) # smallest double that 0.5+epsilon != 0.5
  maxDouble = float(2**1024 - 2**971)  # From the IEEE 754 standard
  minDouble  = math.ldexp(1.0, -1022) # min positive normalized double
  smallEpsilon  = math.ldexp(1.0, -1074) # smallest increment for doubles &lt; minFloat
  infinity = math.ldexp(1.0, 1023) * 2
  
  def nextafter(x,y):    
      """returns the next IEEE double after x in the direction of y if possible"""
      if y==x:
         return y         #if x==y, no increment
  
      # handle NaN
      if x!=x or y!=y:
          return x + y       
  
      if x &gt;= infinity:
          return infinity
  
      if x &lt;= -infinity:
          return -infinity
  
      if -minDouble &lt; x &lt; minDouble:
          if y &gt; x:
              return x + smallEpsilon
          else:
              return x - smallEpsilon  
  
      m, e = math.frexp(x)        
      if y &gt; x:
          m += epsilon
      else:
          m -= epsilon
  
      return math.ldexp(m,e)  
  print nextafter(0.0, -1.0), 'nextafter(0.0, -1.0)'
  print nextafter(-1.0, 0.0), 'nextafter(-1.0, 0.0)'
  </code></pre>
  
  <p>Results of <a href="https://stackoverflow.com/questions/6063755/increment-a-python-floating-point-value-by-the-smallest-possible-amount">Increment a python floating point value by the smallest possible amount</a> is invalid:</p>
  
  <pre><code>&gt;&gt;&gt; nextafter(0.0, -1)
  0.0
  </code></pre>
  
  <p>Should be nonzero.</p>
  
  <pre><code>&gt;&gt;&gt; nextafter(-1,0)
  -0.9999999999999998
  </code></pre>
  
  <p>Should be '-0.9999999999999999'.</p>
  
  <p>(2) It was not asked how to add/substract the smallest value but was asked how to add/substract value in specific direction - propose solution is need to know x and y. Here is required to know only x.</p>
  
  <p>(3) Propose solution in <a href="https://stackoverflow.com/questions/6063755/increment-a-python-floating-point-value-by-the-smallest-possible-amount">Increment a python floating point value by the smallest possible amount</a> will not work on border conditions.</p>