title: Check if double are given with a specific accuracy
body: <p>I have a vector of <code>double</code>. Those numbers are given with a precision such as <code>0.001</code> meaning that the numbers are given with a thousandth of units accuracy. For example I'm expecting to have <code>123456789.012</code> but not <code>123456789.01231</code>. In practice, because of floating-point arithmetic the actual numbers are more like <code>123456789.01199999452</code> which is the valid and closest representation of <code>123456789.012</code>.</p>
  
  <p>I'd like to robustly test if my numbers are actually given with the accuracy there are supposed to be. For example I'd like to warn if I have the <code>123456789.01231</code> which does not match with the accuracy <code>0.001</code>. Another valid option to me is to find the accuracy of a given number.</p>
  
  <p>What I've done so far is to test if <code>(x - offset)/accuracy</code> is an integer. The offset protects against integer overflow. In practice, because of floating-point arithmetic, it cannot be an integer so I added an arbitrary threshold. It works but it is not a robust solution in my opinion.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>dx = (x[i]-offset)/accuracy;
  ix = std::round(dx);
  if (std::abs(dx - ix) &gt; 1e-5)
    throw exception("...");
  </code></pre>
  
  <p>No string is involved in this process. The numbers come from binary files or from runtime computations. The accuracy is given by the user and the numbers are expected to be compliant with this accuracy. In my previous example of <code>123456789.012</code> I do know that this number does not actually exist but <code>123456789.01199999452</code> is valid because it is its best representation with <code>double</code>. But <code>123456789.01231</code> (actually <code>123456789.01230999827</code>) is not valid because it is not the best representation of the 3 digits rounded value.</p>