Sunday, September 24, 2006

WTF?

Does someone want to tell me why "dx = math.sin(1);" returns an accurate number, but "a = 1; dx = math.sin(a);" returns "NaN"?

That is stupid as hell. Seriously, if somebody knows a workaround, I'm all ears. Unless it's to make "a" into a "Number", which I already tried.

Edit: Fascinating. It works if I write "Math.sin" instead of "math.sin". But then why did it work at all? The hell?

This has been your daily dose of stupid fiddly debugging.

2 comments:

Anonymous said...

I'm assuming that code snippet is from ActionScript? Are you using AS1 or AS2? AS1 is not case-sensitive, but AS2 is, thus math.sin() is not valid in AS2 (but is in AS1).

Math.sin() is correct. I've got no idea what you'd get different results... something smells fishy. Is that really all of the relevant code in that snippet?

The only thing I could imagine happening would be how you assigned a value to 'a'. If you said a = "1" (with quotes) then a would be dynamically typed as a string, and thus Math.sin(a) would return NaN.

The safest route would be to type a as a number (var a:Number;) and then you'd be cool. (Or should be!)

Craig Perko said...

Thanks. I figured out that it works fine as Math.sin(whatever).

The weird, weird, weird part is that it ALSO works fine as math.sin(3), but not as math.sin(variable).