How to alter a float by its smallest increment (or close to it)?

前端 未结 7 1198
余生分开走
余生分开走 2020-11-30 02:05

I have a double value f 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

7条回答
  •  既然无缘
    2020-11-30 02:43

    I needed to do the exact same thing and came up with this code:

    double DoubleIncrement(double value)
    {
      int exponent;
      double mantissa = frexp(value, &exponent);
      if(mantissa == 0)
        return DBL_MIN;
    
      mantissa += DBL_EPSILON/2.0f;
      value = ldexp(mantissa, exponent);
      return value;
    }
    

提交回复
热议问题