FreeBasic Quirk

General FreeBASIC programming questions.
newby12
Posts: 33
Joined: Dec 26, 2021 1:57

FreeBasic Quirk

Post by newby12 »

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"
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: FreeBasic Quirk

Post by badidea »

It does a string comparison where "100" < "3" same as "BAA" < "CD"
I cannot find it documentation however.
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: FreeBasic Quirk

Post by adeyblue »

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.
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBasic Quirk

Post by fxm »

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.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic Quirk

Post by caseih »

I guess that's known as lexicographic order. (as fxm says)
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: FreeBasic Quirk

Post by speedfixer »

When 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).
add: ASCII

david
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic Quirk

Post by caseih »

Technically ASCII is 7-bit. But I would expect FB to compare on the full 8-bit byte. Unless using WSTRING of course.
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBasic Quirk

Post by fxm »

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.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBasic Quirk

Post by dodicat »

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
 
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBasic Quirk

Post by fxm »

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]
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: FreeBasic Quirk

Post by grindstone »

newby12 wrote: Mar 13, 2022 1:26 I was playing with numeric strings and found a bug in FB
As you see, it's not a bug, it's a feature! :D
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic Quirk

Post by caseih »

grindstone wrote: Mar 14, 2022 14:11As you see, it's not a bug, it's a feature! :D
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";
Guess what $x is. That's right. It's 13.

Of course even lexicographic sorting can get a bit messy when unicode is involved.
KLBear
Posts: 113
Joined: Jul 23, 2008 9:32

Re: FreeBasic Quirk

Post by KLBear »

comment deleted
Tim Keal
Posts: 3
Joined: May 24, 2020 23:07

Re: FreeBasic Quirk

Post by Tim Keal »

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?) :mrgreen:
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBasic Quirk

Post by dodicat »

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"
Post Reply