code: public static double nextDouble (double d, boolean positive) {
  
          /* filter out NaN's */
          if (Double.isNaN(d)) {
              return d;
          }
  
          /* zero's are also a special case */
          if (d == 0.0) {
              double smallestPositiveDouble = Double.longBitsToDouble(1L);
              if (positive) {
                  return smallestPositiveDouble;
              } else {
                  return -smallestPositiveDouble;
              }
          }
  
          /* if entering here, d is a nonzero value */
  
          /* hold all bits in a long for later use */
          long bits = Double.doubleToLongBits(d);
  
          /* strip off the sign bit */
          long magnitude = bits & ~SIGN;
  
          /* if next double away from zero, increase magnitude */
          if ((bits > 0) == positive) {
              if (magnitude != POSITIVEINFINITY) {
                  magnitude += 1;
              }
          }
          /* else decrease magnitude */
          else {
              magnitude -= 1;
          }
  
          /* restore sign bit and return */
          long signbit = bits & SIGN;
          return Double.longBitsToDouble (magnitude | signbit);
      }
docs: Finds the least double greater than d (if positive == true),
  or the greatest double less than d (if positive == false).
  If NaN, returns same value.
  
  Does not affect floating-point flags,
  provided these member functions do not:
  Double.longBitsToDouble(long)
  Double.doubleToLongBits(double)
  Double.isNaN(double)