Page 1 of 1
Strings and Numbers Comparison Errors
Posted: 27 Mar 2013 00:32
by Enternal
I'm getting odd behaviors with comparisons like:
::msg ("2" < "35a");
::msg ("2" < "52=a");
::msg ("2d" < "52=a");
They all return true. There are also some others too.
Re: Strings and Numbers Comparison Errors
Posted: 27 Mar 2013 01:02
by highend
Yes, seems to be a bug. The help file states that these comparisons should always return false, because (at least) only one part is numeric).
Re: Strings and Numbers Comparison Errors
Posted: 27 Mar 2013 12:52
by TheQwerty

Maybe it's too early for me but I don't see where there is any issue here.
In all three cases at least one side of the expression cannot be treated as a number which causes both sides to be treated as strings.
Since the first character on either side is not the same this is equivalent to comparing the string "2" to the strings "3" and "5". Assuming it uses ASCII then that's comparing 50 to 51 and 53 respectively and it is indeed less than those.
Why do you expect these to return false?
Re: Strings and Numbers Comparison Errors
Posted: 27 Mar 2013 13:33
by highend
Since the first character on either side is not the same this is equivalent to comparing the string "2" to the strings "3" and "5". Assuming it uses ASCII then that's comparing 50 to 51 and 53 respectively and it is indeed less than those.
It's only taking the first character from each string for the comparison, it's not 2 < 3 AND 2 < 5. Apart from that TheQuerty is right, it uses the ascii table (and their decimal value for each character) for the comparison.
E.g.:
msg ("#" < "!");
which would give the result 0, because 35 is not less than 33.
Re: Strings and Numbers Comparison Errors
Posted: 27 Mar 2013 14:00
by TheQwerty
highend wrote:Since the first character on either side is not the same this is equivalent to comparing the string "2" to the strings "3" and "5". Assuming it uses ASCII then that's comparing 50 to 51 and 53 respectively and it is indeed less than those.
It's only taking the first character from each string for the comparison, it's not 2 < 3 AND 2 < 5.
Indeed - I worded it poorly and didn't realize "5" was also the second character in the first expression.
What I meant was in the first case it's comparing the strings "2" vs. "3" and in the other two it's "2" vs. "5".
Also note that you could use the Compare function instead:
Code: Select all
Echo "2" < "10a"; // true;
Echo Compare("2","10a"); // 1 = "2" is greater.
Echo Compare("2","10a", "n"); // -1 = "10a" is greater. This is a numeric comparison of 2 vs. 10.
Echo Compare("10a", "10b", "n"); // 0 = They are equal.
Echo Compare("10a", "a100000", "n"); // 1 = "10a" is greater. Compared 10 vs. 0.
The last three are an important distinction because it is easy to assume the 'n' really means natural which should compare number groups and non-number groups, such as when sorting items in the list you'd want "10a.txt" before "10b.txt", and both before "a100000.txt". However, it actually stands for numeric, which means it tries to convert the string to a number using the left-most characters and stopping when it cannot be done.
Re: Strings and Numbers Comparison Errors
Posted: 27 Mar 2013 19:57
by Enternal
The reason why I expected false is:
If you compare two numerical strings, they are compared as integers:
msg ("2" < "10"); // -> true! (both parts are numeric)
msg ("2" < "10a"); // -> false! (only one parts numeric)
msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
Directly from the manual. So I used that as a reference. So I guess it's a bit more complicated than I thought...
Re: Strings and Numbers Comparison Errors
Posted: 27 Mar 2013 20:43
by TheQwerty
Well let's try to come up with some improvements Don can make to the help...
Would this be better?
If you compare two numerical strings, they will be treated as integers.
If either side is a string, both will be treated as strings and compared character-by-character.
msg ("2" < "10" );
= Shows '1' (True)
= Compared number 2 to number 10.
msg ("2" < "10a");
msg ("2a" < "10a");
= Both show '0' (False)
= Compared character "2" to character "1" and stopped.
msg ("2" < "2b");
= Shows '1' (True)
= Compared character "2" to character "2".
= Because the left side is shorter it is considered "smaller".
msg ("2a" < "2b");
= Shows '1' (True)
= Compared character "2" to character "2".
= Then compared character "a" to character "b" and stopped.
Re: Strings and Numbers Comparison Errors
Posted: 30 Mar 2013 06:06
by Enternal
Ah yes that would be more clear. Hopefully Don reads this when he comes back from break.