title: Numerical regression in x64 with the VS2013 compiler with latest Haswell chips?
body: <p>We recently started seeing unit tests fail on our build machine (certain numerical calculations fell out of tolerance). Upon investigation we found that some of our developers could not reproduce the test failure. To cut a long story short, we eventually tracked the problem down to what appeared to be a rounding error, but that error was only occurring with x64 builds on the latest Haswell chips (to which our build server was recently upgraded). We narrowed it down and pulled out a single calculation from one of our tests:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;cmath&gt;
  
  int _tmain(int argc, _TCHAR* argv[])
  {
    double rate = 0.0021627412080263146;
    double T = 4.0246575342465754;
    double res = exp(-rate * T);
    printf("%0.20e\n", res);
    return 0;
  }
  </code></pre>
  
  <p>When we compile this x64 in VS2013 (with the default compiler switches, including <code>/fp:precise</code>), it gives different results on the older Sandy Bridge chip and the newer Haswell chip. The difference is in the 15th significant digit, which I think is outside the machine epsilon for double on both machines).</p>
  
  <p>If we compile the same code in VS2010 or VS2012 (or, incidentally, VS2013 x86) we get the exact same answer on both chips.</p>
  
  <p>In the past several years, we've gone through many versions of Visual Studio and many different Intel chips for testing, and no-one can recall us ever having to adjust our regression test expectations based on different rounding errors between chips.</p>
  
  <p>This obviously led to a game of whack-a-mole between developers with the older and newer hardware as to what should be the expectation for the tests...</p>
  
  <p>Is there a compiler option in VS2013 that we need to be using to somehow mitigate the discrepancy? </p>
  
  <p>Update: </p>
  
  <pre><code>Results on Sandy Bridge developer PC:
  VS2010-compiled-x64: 9.91333479983898980000e-001
  VS2012-compiled-x64: 9.91333479983898980000e-001
  VS2013-compiled-x64: 9.91333479983898980000e-001
  
  Results on Haswell build server:
  VS2010-compiled-x64: 9.91333479983898980000e-001
  VS2012-compiled-x64: 9.91333479983898980000e-001
  VS2013-compiled-x64: 9.91333479983899090000e-001
  </code></pre>
  
  <p>Update:</p>
  
  <p>I used procexp to capture the list of DLLs loaded into the test program.</p>
  
  <pre><code>Sandy Bridge developer PC:
  apisetschema.dll
  ConsoleApplication8.exe
  kernel32.dll
  KernelBase.dll
  locale.nls
  msvcr120.dll
  ntdll.dll
  
  Haswell build server:
  ConsoleApplication8.exe
  kernel32.dll
  KernelBase.dll
  locale.nls
  msvcr120.dll
  ntdll.dll
  </code></pre>