0

I am trying to convert some code from C++ to JavaScript but I am very confused with ints, doubles, NaN and parsing floats. I think!?

Z = T - 14 - (1);
while (Z <= T)
{
    var S1 = 0.0;
    for (var C = 0; C <= P - 1; C++)
    {
        if (V[Z + C] !== undefined)
        {
            skip = 0;
            S1 = S1 + (V[Z + C] + V[Z + C]) / 2;
        }
        else
        {
            skip = 1;
        }
    }
    console.log('S1: ' + S1);
    Z++;
}

S1 is now always NaN how do I stop this? (something to do with typeof?)

On a good note: I've got the V=[] part to work and that uses doubles and am working on the undefined part (messy fix) so that is some good progress.

David G
  • 94,763
  • 41
  • 167
  • 253
BENZ.404
  • 401
  • 1
  • 5
  • 20
  • Thanks Neil for the edit but, before you have a go at someone else look first at your self mate http://stackoverflow.com/questions/3218756/javascript-braces-on-new-line-or-not see the answer in that post about Douglas Crockford. And this is a question and answer forum not a question and insult forum. – BENZ.404 Apr 14 '14 at 22:22
  • Could you post the original C++ code? – thus spake a.k. Apr 14 '14 at 22:26

1 Answers1

1

When an operation involving floats fails, NaN is the resulting value. NaN has an interesting property: operating with NaN always results in NaN.

This means that as soon as one of your variables holds NaN, the value will propagate with all future operations.

console.log your floats, and detect the moment in which one becomes NaN. My bet is in (V[Z + C] + V[Z + C]).

salezica
  • 74,081
  • 25
  • 105
  • 166