gremlins in my c++ method
<p>I've traced a bug down to a function which should be returning float values between 20 and 100 or so, but is sometimes (1 time in 10) returning values much much higher than that. The problem exists when  I have an expression in the last line of the method, like this:</p>

<pre><code>return snap(baseNumber, targets) + (octave * NOTES_PER_OCTAVE);
</code></pre>

<p>If I store the return value in a variable first, then return that variable, the problem goes away:</p>

<pre><code>float ret = snap(baseNumber, targets) + (octave * NOTES_PER_OCTAVE);
return ret;
</code></pre>

<p>Here's the complete method:</p>

<pre><code>static inline float octaveSnap(float number, std::vector&lt;float&gt;* targets){
    static const int NOTES_PER_OCTAVE = 12;
    int octave = number / NOTES_PER_OCTAVE;
    float baseNumber = number - (octave * NOTES_PER_OCTAVE);
    float ret = snap(baseNumber, targets) + (octave * NOTES_PER_OCTAVE);
    return ret;
}
</code></pre>

<p>and here's 'snap':</p>

<pre><code>    // given a single value and a list of values (a scale), return the member of the list which is closest to the single value 
static inline float snap(float number, std::vector&lt;float&gt;* targets){
    float ret;
    float leastDistance = -1;
    for(int i = 0; i&lt;targets-&gt;size(); i++){
        float distance = targets-&gt;at(i) - number;
        if(distance &lt; 0){
            distance = -distance;
        }
        if(leastDistance == -1){
            leastDistance = distance;
        }
        if(distance &lt; leastDistance){
            leastDistance = distance;
            ret = targets-&gt;at(i);
        }
    }
    return ret;
}
</code></pre>

<p>I'm completely baffled by this. Any idea why the first explodes and the second works perfectly?</p>
