Strings and Numbers Comparison Errors

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
Enternal
Posts: 1175
Joined: 10 Jan 2012 18:26

Strings and Numbers Comparison Errors

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

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Strings and Numbers Comparison Errors

Post 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).
One of my scripts helped you out? Please donate via Paypal

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Strings and Numbers Comparison Errors

Post by TheQwerty »

:veryconfused: 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?

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Strings and Numbers Comparison Errors

Post 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.
One of my scripts helped you out? Please donate via Paypal

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Strings and Numbers Comparison Errors

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

Enternal
Posts: 1175
Joined: 10 Jan 2012 18:26

Re: Strings and Numbers Comparison Errors

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

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Strings and Numbers Comparison Errors

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

Enternal
Posts: 1175
Joined: 10 Jan 2012 18:26

Re: Strings and Numbers Comparison Errors

Post by Enternal »

Ah yes that would be more clear. Hopefully Don reads this when he comes back from break.

Post Reply