<p>So I have a function that looks something like this:</p>

<pre><code>float function(){
    float x = SomeValue;
    return x / SomeOtherValue;
}
</code></pre>

<p>At some point, this function overflows and returns a really large negative value.  To try and track down exactly where this was happening, I added a cout statement so that the function looked like this:</p>

<pre><code>float function(){
    float x = SomeValue;
    cout &lt;&lt; x;
    return x / SomeOtherValue;
}
</code></pre>

<p>and it worked!  Of course, I solved the problem altogether by using a double.  But I'm curious as to why the function worked properly when I couted it.  Is this typical, or could there be a bug somewhere else that I'm missing?</p>

<p>(If it's any help, the value stored in the float is just an integer value, and not a particularly big one.  I just put it in a float to avoid casting.)</p>

[{'answer_id': 49120, 'author': 'David Dibben', 'author_id': 5022, 'author_profile': 'https://Stackoverflow.com/users/5022', 'pm_score': 2, 'selected': False, 'text': "<p>Printing a value to cout should not change the value of the paramter in any way at all.</p>\n\n<p>However, I have seen similar behaviour, adding debugging statements causes a change in the value. In those cases, and probably this one as well my guess was that the additional statements were causing the compiler's optimizer to behave differently, so generate different code for your function.</p>\n\n<p>Adding the cout statement means that the vaue of x is used directly. Without it the optimizer could remove the variable, so changing the order of the calculation and therefore changing the answer.</p>\n"}, {'answer_id': 49121, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 0, 'selected': False, 'text': '<p>I dont think the cout has any effect on the variable, the problem would have to be somewhere else.</p>\n'}, {'answer_id': 49130, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 5, 'selected': True, 'text': '<p>Welcome to the wonderful world of floating point.  The answer you get will likely depend on the floating point model you compiled the code with.  </p>\n\n<p>This happens because of the difference between the IEEE spec and the hardware the code is running on.  Your CPU likely has 80 bit floating point registers that get use to hold the 32-bit float value. This means that there is far more precision while the value stays in a register than when it is forced to a memory address (also known as \'homing\' the register).</p>\n\n<p>When you passed the value to cout the compiler had to write the floating point to memory, and this results in a lost of precision and interesting behaviour WRT overflow cases.</p>\n\n<p>See the MSDN documentation on VC++ <a href="http://msdn.microsoft.com/en-us/library/e7s85ffb(VS.80).aspx" rel="noreferrer">floating point switches</a>.  You could try compiling with /fp:strict and seeing what happens.</p>\n'}, {'answer_id': 50068, 'author': 'David Joyner', 'author_id': 1146, 'author_profile': 'https://Stackoverflow.com/users/1146', 'pm_score': 2, 'selected': False, 'text': "<p>As an aside, it's always a good idea to declare immutable variables using <code>const</code>:</p>\n\n<pre><code>float function(){\n    const float x = SomeValue;\n    cout &lt;&lt; x;\n    return x / SomeOtherValue;\n}\n</code></pre>\n\n<p>Among other things this will prevent you from unintentionally passing your variables to functions that may modify them via non-<code>const</code> references.</p>\n"}, {'answer_id': 3607991, 'author': 'Joe', 'author_id': 435796, 'author_profile': 'https://Stackoverflow.com/users/435796', 'pm_score': 1, 'selected': False, 'text': "<p>cout causes a reference to the variable, which often will cause the compiler to force it to spill it to the stack.</p>\n\n<p>Because it is a float, this likely causes its value to be truncated from the double or long double representation it would normally have.</p>\n\n<p>Calling any function (non-inlined) that takes a pointer or reference to x should end up causing the same behavior, but if the compiler later gets smarter and learns to inline it, you'll be equally screwed :)</p>\n"}]
['https://Stackoverflow.com/questions/49098', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2147/']