title: Get next smallest nearest number to a decimal
body: <p><strong>Introduction</strong></p>
  
  <p>For some calculations I need to find the smallest possible number I can add/subtract from a specified number without JavaScript getting in trouble with the internal used data type.</p>
  
  <p><strong>Goal</strong></p>
  
  <p>I tried to write a function which is able to return the next nearest number to VALUE in the direction of value DIR.</p>
  
  <pre class="lang-js prettyprint-override"><code>function nextNearest(value, direction) {
      // Special cases for value==0 or value==direction removed
  
      if (direction &lt; value) {
          return value - Number.MIN_VALUE;
      } else {
          return value + Number.MIN_VALUE;
      }
  }
  </code></pre>
  
  <p>The problem with this is, that JavaScript uses a 64-bit float type (I think) which has different minimum step sizes depending on its current exponent.</p>
  
  <p><strong>Problem in detail</strong></p>
  
  <p>The problem is the step size depending on its current exponent:</p>
  
  <pre class="lang-js prettyprint-override"><code>var a = Number.MIN_VALUE;
  
  console.log(a);
  // 5e-324
  
  console.log(a + Number.MIN_VALUE);
  // 1e-323 (changed, as expected)
  
  
  var a = Number.MAX_VALUE;
  
  console.log(a);
  // 1.7976931348623157e+308
  
  console.log(a - Number.MIN_VALUE);
  // 1.7976931348623157e+308 (that's wrong)
  
  console.log(a - Number.MIN_VALUE == a);
  // true (which also is wrong)
  </code></pre>
  
  <p><strong>Summary</strong></p>
  
  <p>So how can I find the smallest possible number I can add/subtract from a value specified in a parameter in any direction? In C++ this would be easily possible by accessing the numbers binary values.</p>