title: Wrapping floats counter in C++
body: <p>This question refers to <a href="https://stackoverflow.com/questions/707370/clean-efficient-algorithm-for-wrapping-integers-in-c">this one</a>, but applying to floating point numbers too. </p>
  
  <p>In short, if count reaches min or max bound it should wrap to opposite bound taking into account remainder of step value overflow (for instance if bounds are <code>0 ... 10.5</code>, <code>value</code> is 3 and <code>step</code> is -7.5). Also we should handle case when <code>step</code> value is greater than <code>range</code> value. </p>
  
  <p>Ranges may be arbitrary (negative, positive or mixed) but taking into account given asserts.</p>
  
  <p>So far I have this method which works for integers, but gives wrong result for doubles whenever <code>step</code> is fractional.</p>
  
  <p>Also not sure we should use epsilon approach here while comparing doubles.</p>
  
  <p>Question is how to implement this method properly for fractional values.</p>
  
  <p><a href="http://cpp.sh/2yhol" rel="nofollow noreferrer">Runnable code</a></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;assert.h&gt;
  
  double
  countInRange(const double step, const double value, const double minValue, const double maxValue)
  {
      assert(minValue &lt; maxValue);
      assert(value &gt;= minValue &amp;&amp; value &lt;= maxValue);
  
      auto range = maxValue - minValue + 1;
      assert(fabs(step) &lt;= range);
  
      auto result = value + step;
  
      if (result &lt; minValue) {
          result += range;
      } else if (result &gt; maxValue) {
          result -= range;
      }
  
      return result;
  }
  
  int main()
  {
      double v = 358;
  
      printf("increment\n");
  
      for (int i = 0; i &lt; 10; i++) {
          v = countInRange(0.5, v, 0, 359.5);
          printf("%f\n", v);
      }
  
      printf("decrement\n");
  
      v = 2;
  
      for (int i = 0; i &lt; 10; i++) {
          v = countInRange(-0.5, v, 0, 359.5);
          printf("%f\n", v);
      }
  }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>So I ended up with two methods for different interval logics, the first one taken from answer of Superlokkus, they may not cover all cases, but for now it's enough, thanks to all the commenters</p>
  
  <pre><code>    /**
       * Wraps to minValue or maxValue plus step using half-open interval logic
       */
      double
      countInRange(const double step, const double value, const double minValue, const double maxValue)
      {
          assert(minValue &lt; maxValue);
          assert(value &gt;= minValue &amp;&amp; value &lt;= maxValue);
  
          auto range = maxValue - minValue + 1;
  
          assert(fabs(step) &lt;= range);
  
          auto result = value + step;
  
          if (result &lt; minValue) {
              result = maxValue - abs(minValue - result);
          } else if (result &gt; maxValue) {
              result = minValue + (result - maxValue);
          }
  
          return result;
      }
      /**
       * Wraps to minValue or maxValue using closed interval logic
       */
      double
      countInRange(const int step, const double value, const double minValue, const double maxValue)
      {
          assert(minValue &lt; maxValue);
          assert(value &gt;= minValue &amp;&amp; value &lt;= maxValue);
  
          auto range = maxValue - minValue + 1;
  
          assert(abs(step) &lt;= range);
  
          auto result = value + step;
  
          if (result &lt; minValue) {
              result += range;
          } else if (result &gt; maxValue) {
              result -= range;
          }
  
          return result;
      }
  </code></pre>