title: How to alter a float by its smallest increment in Java?
body: <p>I have a <code>double</code> value <code>d</code> and would like a way to nudge it very slightly larger (or smaller) to get a new value that will be as close as possible to the original but still strictly greater than (or less than) the original.</p>
  
  <p>It doesn't have to be close down to the last bit—it's more important that whatever change I make is guaranteed to produce a different value and not round back to the original.</p>
  
  <p><a href="https://stackoverflow.com/questions/2623718/java-for-loop-pre-increment-vs-post-increment-closed">(This question has been asked and answered for C, C++)</a></p>
  
  <p>The reason I need this, is that I'm mapping from Double to (something), and I may have multiple items with the save double 'value', but they all need to go individually into the map.</p>
  
  <p>My current code (which does the job) looks like this:</p>
  
  <pre>
  <code>
  private void putUniqueScoreIntoMap(TreeMap map, Double score,
              A entry) {
  
          int exponent = 15;
          while (map.containsKey(score)) {
              Double newScore = score;
              while (newScore.equals(score) && exponent != 0) {
                  newScore = score + (1.0d / (10 * exponent));
                  exponent--;
              }
              if (exponent == 0) {
                  throw new IllegalArgumentException("Failed to find unique new double value");
              }
              score = newScore;
          }
          map.put(score, entry);
      }
  </code>
  </pre>