title: Strange rounding behavior
body: <p>I'm trying to round doubles to a specific precision, however the following functions give me different results:</p>
  
  <p>Version 1:</p>
  
  <pre><code>static double RoundPrecision(double&amp; val)
  {
      val = floor(val * 1000 + 0.5) * 0.001;
      return val;
  }
  </code></pre>
  
  <p>Version 2:</p>
  
  <pre><code>static double RoundPrecision(double&amp; val)
  {
      val = floor(val * 1000 + 0.5) / 1000;
      return val;
  }
  </code></pre>
  
  <p>Example output when rounding the number 300.9:</p>
  
  <ul>
  <li>Version 1: 300.90000000000003</li>
  <li>Version 2: 300.89999999999998</li>
  </ul>
  
  <p>Both versions sometimes give the same result, but for specific inputs the results differ. I have to have consistent behavior when equating numbers to other variables in the program.</p>
  
  <p>EDIT:</p>
  
  <p>I am aware of the problems with floating point precision, which is exactly what I'm trying to avoid here by rounding. I need a consistent way to round to 3 decimal point precision.</p>