title: If two languages follow IEEE 754, will calculations in both languages result in the same answers?
body: <p>I'm in the process of converting a program from Scilab code to C++. One loop in particular is producing a slightly different result than the original Scilab code (it's a long piece of code so I'm not going to include it in the question but I'll try my best to summarise the issue below).</p>
  
  <p>The problem is, each step of the loop uses calculations from the previous step. Additionally, the difference between calculations only becomes apparent around the 100,000th iteration (out of approximately 300,000).</p>
  
  <p><em>Note: I'm comparing the output of my C++ program with the outputs of Scilab 5.5.2 using the "format(25);" command. Meaning I'm comparing 25 significant digits. I'd also like to point out I understand how precision cannot be guaranteed after a certain number of bits but read the sections below before commenting. So far, all calculations have been identical up to 25 digits between the two languages.</em> </p>
  
  <p>In attempts to get to the bottom of this issue, so far I've tried:</p>
  
  <ol>
  <li><strong>Examining the data type being used:</strong></li>
  </ol>
  
  <p>I've managed to confirm that Scilab is using IEEE 754 doubles (according to the language documentation). Also, according to Wikipedia, C++ <em>isn't</em> required to use IEEE 754 for doubles, but from what I can tell, everywhere I use a double in C++ it has perfectly match Scilab's results.</p>
  
  <ol start="2">
  <li><strong>Examining the use of transcendental functions:</strong></li>
  </ol>
  
  <p>I've also read from <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#873">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a> that IEEE does not require transcendental functions to be exactly rounded. With that in mind, I've compared the results of these functions (sin(), cos(), exp()) in both languages and again, the results appear to be the same (up to 25 digits).</p>
  
  <ol start="3">
  <li><strong>The use of other functions and predefined values:</strong></li>
  </ol>
  
  <p>I repeated the above steps for the use of sqrt() and pow(). As well as the value of Pi (I'm using M_PI in C++ and %pi in Scilab). Again, the results were the same.</p>
  
  <ol start="4">
  <li><strong>Lastly, I've rewritten the loop (very carefully) in order to ensure that the code is identical between the two languages.</strong></li>
  </ol>
  
  <hr>
  
  <p><em>Note: Interestingly, I noticed that for all the above calculations the results between the two languages match farther than the actual result of the calculations (outside of floating point arithmetic). For example:</em></p>
  
  <p><em>Value of sin(x) using Wolfram Alpha = 0.123456789.....</em></p>
  
  <p><em>Value of sin(x) using Scilab &amp; C++ = 0.12345yyyyy.....</em></p>
  
  <p><em>Where even once the value computed using Scilab or C++ started to differ from the actual result (from Wolfram). Each language's result still matched each other. This leads me to believe that most of the values are being calculated (between the two languages) in the same way. Even though they're not required to by IEEE 754.</em></p>
  
  <hr>
  
  <p>My original thinking was one of the first three points above are implemented differently between the two languages. But from what I can tell everything seems to produce identical results.</p>
  
  <p><strong>Is it possible that even though all the inputs to these loops are identical, the results can be different? Possibly because a very small error (past what I can see with 25 digits) is occurring that accumulates over time? If so, how can I go about fixing this issue?</strong></p>