I'm sure this basic question has been answered before but my forum searching isn't finding it.
Apparently if...NaN isn't part of the current KRP syntax?
I'm sure this basic question has been answered before but my forum searching isn't finding it.
Apparently if...NaN isn't part of the current KRP syntax?
Check out this thread, maybe it will be of some help
if( testvariable conditions fail to evaluate for string variables etc
Make a function that adds 1 to the variable and if it returns NaN, that sucker ain't a number.
Ok lets try some off the cuff stuff. If it doesn't work, it should be easy to fix
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
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?
Howdy Sacha,
Thinking along the same lines! Except, as I found, you can't test the NaN result directly.
You're right!
Although on my tests, copy works. But I'm using the javascript console. Sometimes, things don't work the same.
But I'm using the javascript console. Sometimes, things don't work the same.
Yup. Which can lead to rewarding hours hair pulling at times. Maybe that's why I no longer have any!
Klaus -- if you are listening, is this right? Maybe you can add an eval for NaN or am I missing something?
Hi,
okay, I have added this is as feature request - I will think about a way for testing for NaN.
Best regards,
Klaus
You're the best, Klaus.
Display Moreset(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
Thanks Scott Witte
I tried your code but I think it dosen't work properly. At the end you say "if(test2==NaN, == will be true" perphaps it would be true but unfortunately it doesn't.
Inspired in your suggestion I did this other action I think work fine
<action name="test_numeric">
set(test,calc(%1^2));
if( test GT 0 , set(isNumber,true),set(isNumber,false));
trace (isNumber);
</action>
The advantage of this code is that you only get a value of true when the variable you pass to the action exist and is a number. If the var you pass is a string, is not define, or is null, the value of isNumber is false.
Hi,
that thread is outdated, krpano already has a build-in isNaN function:
https://krpano.com/docu/actions/#expressions.inlinefunctions
E.g. just:
Best regards,
Klaus
that thread is outdated, krpano already has a build-in isNaN function:
https://krpano.com/docu/actions/#expressions.inlinefunctions
I knew the function "isNanN", but for me the problem of this function is this:
it works fine with strings
set(test,"b");
if(isNaN(test),trace('Its NOT number'),trace('Its a number')); ==> its NOT a number
or numbers
set(test,"4"); if(isNaN(test),trace('Its NOT number'),trace('Its a number')); ==> its a number
but when your have a empty, or not defined variable.
set(test,"");
if(isNaN(test),trace('Its NOT number'),trace('Its a number')); ==> its a number
set(test," ");
if(isNaN(test),trace('Its NOT number'),trace('Its a number')); ==> its a number
and if test is not defined
if(isNaN(test),trace('Its NOT number'),trace('Its a number')); ==> its a number
Best regards,
deDaimiel
Hi,
that's how isNaN() is working:
https://developer.mozilla.org/en-US/docs/Web…NaN#Description
Empty strings are parsed as 0 value and 0 is a number...
But it would be possible to change the isNaN() function to the Javascript Number.isNaN() function, then checks for undefined variables would result in false.
Best regards,
Klaus
Don’t have an account yet? Register yourself now and be a part of our community!