Thanks Alexey,
Before asking my question I read that and spent a couple hours creating and testing my own if statements trying to resolve the simple but important condition,
If(a variable is a number, then... because there is no NaN test this just isn't easy.
Here is the workaround I eventually stumbled on. Maybe it will help someone else. (I really can't believe this question hasn't been asked dozens of times before.) If you multiply a variable that is undefined or isn't a number by a number the result is Not a Number (NaN). So if you have a variable, testcase, that isn't a number:
mul(test, testcase, 2);
trace(get(test)); ==> NaN
Easy! But this is a condition, not a string so if you test,
if(test==NaN, ...
it will be false. The workaround is to convert the condition NaN into a string, 'NaN' and test that. So
set(test2, get(test));
if(test2==NaN,...
evaluates correctly. Curiously, even though the docs say, "The copy() action copies the content from one variable to the other one.
This is the same like: set(destination, get(source) )".... that isn't exactly true. If you
copy(test2, test) test2 will evaluate exactly like test. In other words it seems to copy the condition while set() converts the condition into a string. (Is this a bug or a feature?) So
|
Source code
|
1
2
3
4
5
6
7
|
set(testcase, t);
mul(test, testcase, 2);
trace(get(test)); ==> NaN
if(test==NaN,... ==> tests false
set(test2,test);
trace(get(test2)); ==> NaN
if(test2==NaN, ==> will be true
|
This is the only way I found to test if a variable is a number.
Klaus -- if you are listening, is this right? Maybe you can add an eval for NaN or am I missing something?