title: Multiplying floats and keep/get double precision accuracy
body: <p>I have a function that takes floats, I'm doing some computation with them, and I'd like to keep as much accuracy as possible in the returned result. I read that when you multiply two floats, you double the number of significant digits.</p>
  
  <p>So when two floats get multiplied, for example <code>float e, f;</code> and I do <code>double g = e * f</code>, when do the bits get truncated?</p>
  
  <p>In my example function below, do I need casting, and if yes, where? This is in a tight inner loop, if I put <code>static_cast&lt;double&gt;(x)</code> around each variable <code>a b c d</code> where it's used, I get 5-10% slowdown. But I suspect I don't need to cast each variable separately, and only in some locations, if at all? Or does returning a double here do not give me any gain anyway and I can as well just return a float?</p>
  
  <pre><code>double func(float a, float b, float c, float d) {
      return (a - b) * c + (a - c) * b;
  }
  </code></pre>