FreeBasic Quirk
FreeBasic Quirk
I was playing with numeric strings and found a bug in FB
s1 = "21"
s2 = "31
if s1 <= "3" then print "1"
It thinks s1 = "2" instead of "21"
s1 = "21"
s2 = "31
if s1 <= "3" then print "1"
It thinks s1 = "2" instead of "21"
Re: FreeBasic Quirk
It does a string comparison where "100" < "3" same as "BAA" < "CD"
I cannot find it documentation however.
I cannot find it documentation however.
Re: FreeBasic Quirk
If you want some googleable terms:
Freebasic (and pretty much every language with strings) does a lexicographic/alphabetic sort by default, which means "File100" will sort before "File20" because the Asc() value of '1' is less than the Asc value of '2'
A sort that makes the numbers in strings sort numerically so that "File20" comes before "File100" is generally called a logical sort, and usually requires custom code in low-level native languages because the C libraries that so many use for their string handling doesn't have any capacity for logical sorting.
Freebasic (and pretty much every language with strings) does a lexicographic/alphabetic sort by default, which means "File100" will sort before "File20" because the Asc() value of '1' is less than the Asc value of '2'
A sort that makes the numbers in strings sort numerically so that "File20" comes before "File100" is generally called a logical sort, and usually requires custom code in low-level native languages because the C libraries that so many use for their string handling doesn't have any capacity for logical sorting.
Re: FreeBasic Quirk
I propose to add a same short sentence for each concerned relational operator ('<', '<=', '>', '>='), to specify its behavior when applied to string type data:
[edit]
Sentence reworded.
When the operator is applied to string type data, then a lexicographic/alphabetic order comparison is performed (ordered from the numeric values of the characters of the two strings).
[edit]
Sentence reworded.
Re: FreeBasic Quirk
I guess that's known as lexicographic order. (as fxm says)
-
- Posts: 606
- Joined: Nov 28, 2012 1:27
- Location: CA, USA moving to WA, USA
- Contact:
Re: FreeBasic Quirk
add: ASCIIWhen the operator is applied to string type data, then a lexicographic/alphabetic order comparison is performed (ordered from the numeric ASCII values of the characters of the two strings).
david
Re: FreeBasic Quirk
Technically ASCII is 7-bit. But I would expect FB to compare on the full 8-bit byte. Unless using WSTRING of course.
Re: FreeBasic Quirk
ASCII code (7/8-bit) is only for a String character or Zstring character.
For a Wstring, an unicode integer (16/32-bit) represents each character.
For a Wstring, an unicode integer (16/32-bit) represents each character.
Re: FreeBasic Quirk
Compare numerical strings:
Code: Select all
Function isgreater(s As String,f As String) As Long
If Len(s)>Len(f) Then Return -1
If Len(s)<Len(f) Then Return 0
If s>f Then Return -1 Else Return 0
End Function
function isless(s as string,f as string) as long
if isgreater(s,f) then return 0
return -1
end function
function isequal(s as string,f as string) as long
return s=f
end function
sub compare(s as string,f as string)
if isequal(s,f) then print s;tab(10); " is equal to ";f
if isgreater(s,f) then print s;tab(10);" is greater than ";f
if isless(s,f) then print s;tab(10); " is less than ";f
end sub
for n as long=1 to 20
var s1 = str(int(rnd*200))
var s2 = str(int(rnd*200))
compare(s1,s2)
next n
sleep
Re: FreeBasic Quirk
fxm wrote: ↑Mar 13, 2022 9:03 I propose to add a same short sentence for each concerned relational operator ('<', '<=', '>', '>='), to specify its behavior when applied to string type data:When the operator is applied to string type data, then a lexicographic/alphabetic order comparison is performed (ordered from the numeric values of the characters of the two strings).
[edit]
Sentence reworded.
Done:
- KeyPgOpGreaterThan → fxm [behavior with string type data]
- KeyPgOpGreaterThanOrEqual → fxm [behavior with string type data]
- KeyPgOpLessThanOrEqual → fxm [behavior with string type data]
- KeyPgOpLessThan → fxm [behavior with string type data]
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: FreeBasic Quirk
Not only that, it's a feature of nearly all sane programming languages. If you want a programming language that acts more like the OP expects, consider PHP. Shudder. Consider this beauty:
Code: Select all
$x = 3 + "10% discount";
Of course even lexicographic sorting can get a bit messy when unicode is involved.
Re: FreeBasic Quirk
comment deleted
Re: FreeBasic Quirk
Here is my "Keal's Natural Order Demo (FBC)"
http://puzzlum.org/doors/?prj=Keal%27s% ... out&flav=D
(Did my entry win the contest?)
http://puzzlum.org/doors/?prj=Keal%27s% ... out&flav=D
(Did my entry win the contest?)
Re: FreeBasic Quirk
Hi Tim Keal
There are tons of errors, did you miss some code out?
(I copied and pasted your .txt from the link, is there another link I missed?)
Thanks.
fbc 1.09.0, win 10
OK I have the link now.
I tested
a: 11
b: 9
result
ascii: "11" < "9"
. . .
. . .
natural: "11" < "9"
(I copied and pasted your .txt from the link, is there another link I missed?)
Thanks.
fbc 1.09.0, win 10
OK I have the link now.
I tested
a: 11
b: 9
result
ascii: "11" < "9"
. . .
. . .
natural: "11" < "9"