You are not logged in.

Scott Witte

Intermediate

  • "Scott Witte" started this thread

Posts: 309

Location: Milwaukee, WI USA

Occupation: Professional Photographer

  • Send private message

1

Saturday, October 25th 2014, 11:38pm

How to test if a variable is a number

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?

Alexey Tkachenko

Professional

Posts: 760

Location: Sevastopol, Crimea, Russian Federation

Occupation: Interpreting, Building virtual tours

  • Send private message

2

Sunday, October 26th 2014, 10:49am

Check out this thread, maybe it will be of some help

if( testvariable conditions fail to evaluate for string variables etc
Regards,

Alexey

Posts: 1,852

Occupation: Virtual Tours - Photography - Krpano developer

  • Send private message

3

Sunday, October 26th 2014, 9:17pm

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

Source code

1
2
3
4
5
6
7
8
9
10
11
<action name="test_numeric">
add(tvar_test_numeric,%1,1);
ifnot (tvar_test_numeric == NaN, set(is_numeric, true);,set(is_numeric,false);
</action>

set(myvar, 6);
test_numeric(get(myvar));
trace(is_numeric);
set(myvar, 6o);
test_numeric(get(myvar));
trace(is_numeric);
KRPano Developer: Portfolio ::Gigapixel Tagging Solutions - Porfolio 2 :: Facebook :: Twitter :: reddit.com/r/VirtualTour

Scott Witte

Intermediate

  • "Scott Witte" started this thread

Posts: 309

Location: Milwaukee, WI USA

Occupation: Professional Photographer

  • Send private message

4

Sunday, October 26th 2014, 9:19pm

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?

Scott Witte

Intermediate

  • "Scott Witte" started this thread

Posts: 309

Location: Milwaukee, WI USA

Occupation: Professional Photographer

  • Send private message

5

Sunday, October 26th 2014, 9:22pm

Howdy Sacha,

Thinking along the same lines! Except, as I found, you can't test the NaN result directly.

Posts: 1,852

Occupation: Virtual Tours - Photography - Krpano developer

  • Send private message

6

Sunday, October 26th 2014, 9:34pm

You're right!
Although on my tests, copy works. But I'm using the javascript console. Sometimes, things don't work the same.
KRPano Developer: Portfolio ::Gigapixel Tagging Solutions - Porfolio 2 :: Facebook :: Twitter :: reddit.com/r/VirtualTour

Scott Witte

Intermediate

  • "Scott Witte" started this thread

Posts: 309

Location: Milwaukee, WI USA

Occupation: Professional Photographer

  • Send private message

7

Sunday, October 26th 2014, 10:10pm

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!

8

Monday, October 27th 2014, 11:47am

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

Scott Witte

Intermediate

  • "Scott Witte" started this thread

Posts: 309

Location: Milwaukee, WI USA

Occupation: Professional Photographer

  • Send private message

9

Monday, October 27th 2014, 3:28pm

You're the best, Klaus.

10

Tuesday, February 18th 2020, 7:55pm

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
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



Source code

1
2
3
4
5
<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.

11

Tuesday, February 18th 2020, 9:21pm

javascript is your friend *rolleyes*

Source code

1
2
3
<action name="isnumber" type="javascript">
	krpano.set(args[1], !isNaN(Number(args[2])));
</action>

12

Tuesday, February 18th 2020, 9:39pm

Hi,

that thread is outdated, krpano already has a build-in isNaN function:
https://krpano.com/docu/actions/#expressions.inlinefunctions

E.g. just:

Source code

1
if(isNaN(test), ...);


Best regards,
Klaus

13

Wednesday, February 19th 2020, 5:43pm

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

14

Wednesday, February 19th 2020, 11:42pm

Hi,

that's how isNaN() is working:
https://developer.mozilla.org/en-US/docs…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