Home
Home
Apps
Apps
Ask
Ask
Manual
Manual
Share
Share

Type comparison buggy


#1

Behavior of isNumber:

str="7"
a=isNumber(str)
b=isNumber(+str)
Return(a,b)

Result: a=0, b=1

So, if one wants to extract numbers from a string, one has to use the unary “+” for testing, wether the string (or part of) is numeric.

But unfortunately this test goes wrong in case of a not numeric string:

    str="some string"
    a=isNumber(str)
    b=isNumber(+str)
    Return(a,b)

Result: a=0, b=1

So: isNumber(+"Hello world") returns 1 …

A variant of isNumber works better:

str="7"
a=0
b=0
if str is Number
   a=1
end
if +str is Number
   b=1
end
Return(a, b)

Result: a=1, b=1

But …

str="some string"
a=0
b=0
if str is Number
   a=1
end
if +str is Number
   b=1
end
Return(a, b)

Result: a=1, b=0

So the test of a numeric part in a string works correct, if one uses the construction “if … is Number” with unary +.

Things would be simpler and clearer if the expression isNumber(+"some string") would return 0 instead of 1.

Fix in one of the next releases?


#2

So, if one wants to extract numbers from a string, one has to use the unary “+” for testing, wether the string (or part of) is numeric.

I do not think the issue is with isNumber, but with your use of the “+” operator. I do not believe it is designed to work with strings quite that way. Is there some documentation on this feature?

In any case, isNumber(+“any string you want to put here”) will always return 1 because of the way MS evaluates +“any string you want to put here”. Put +“any string you want to put here” in a standalone expression and you’ll see it always evaluates to 0, since the string is evaluated and returns a 0.

+"any string you want to put here"
0

Naturally 0 is a number, so isNumber will always return a 1.

I am not sure what +“string” is really defined to do. Any mention in the docs? But I agree, it might be better if a NAN or perhaps just the string itself were returned here for mathematical operations on non-numeric strings.

Interestingly, playing around with simple operations on strings produces some very odd results!


#3

Hello arsastronautica,

thank you for taking care of my problem. I agree with you, that the behavior of “+” on strings causes my trouble. I have to acquit the isNumber command.

Perhaps I my ask you a further question: What is in your opinion the best way of testing, wether a string is a number or not?

The problem arises from writing a parser, witch splits strings like "12a3bc23a" into a more structured list [[12,"a"],[3,"b"],[1,"c"],[23,"a"]] and therefore has to identify the numeric parts in the string.

Best regards
Robert