|
Post by eddavis2 on Sept 19, 2022 11:11:33 GMT -6
I ran into the following problems with RCBasic:
1) Strings aren't always compared as one would expect:
s$ = "4"
if s$ >= "0" then print s$; " is >= 0" else print s$; " is < 0 - oops" end if
if s$ <= "9" then print s$; " is <= 9" else print s$; " is > 9 - oops" end if
if "4" >= "0" then print "4 is >= 0" else print "4 is < 0 - oops" end if
if "4" <= "9" then print "4 is <= 9" else print "4 is > 9 - oops" end if
When I run this I get:
2) instr() returns a large positive integer on failure:
p = instr("abcd", "e") print p
3) RCBasic compiler barfs on negative numbers sometimes, especially leading ones it seems. These are some of the unit tests for my Tiny Basic Interpreter, that RCBasic could not compile:
if (-(1 + 2)) <> -3 then: print " 79 failed": e = e + 1: end if if (-(-2 * -6)) <> -12 then: print " 86 failed": e = e + 1: end if if (2 *- 5 + 3) <> -7 then: print " 94 failed": e = e + 1: end if if (-1^ 2 * - -1) <> -1 then: print "101 failed": e = e + 1: end if if (-(-3)^2) <> -9 then: print "104 failed": e = e + 1: end if
|
|
|
Post by n00b on Sept 19, 2022 18:06:25 GMT -6
Thanks for the bug reports. Its much appreciated. 1) Strings aren't always compared as one would expect: That is honestly something I have never even tried to do. Its probably an easy fix. I will see if I can squeeze this into the next release 2) instr() returns a large positive integer on failure: p = instr("abcd", "e") print p
This function should be returning -1 as a double (64-bit float). That number is actually the value of -1 in a 32-bit signed integer. Its an easy fix so I can get that in the next release. 3) RCBasic compiler barfs on negative numbers sometimes, especially leading ones it seems. These are some of the unit tests for my Tiny Basic Interpreter, that RCBasic could not compile: This is actually an issue with the tokenizer/parser which is some serious spaghetti code. I will add this to my list of things to address in 3.20 but changing that code will probably lead to other bugs so I don't think trying to address it so close to next release.
|
|
|
Post by eddavis2 on Sept 19, 2022 18:20:47 GMT -6
How would you do this, e.g.: You have a character in a string, s$.. You want to know if that character is the start of an identifier, and integer, punctuation, etc.
I got this to work:
if asc(s$) >= asc("0") and asc(s$) <= asc("9") then ..
But what is the proper way to do this in RCBasic?
Thanks!
|
|
|
Post by n00b on Sept 19, 2022 18:44:11 GMT -6
I think the way you did it here is the best way to do it in RCBasic currently. You could do some optimization on your code though:
Run this and look at the difference. Its 3 to 4 times faster to just store your min and max character.
s$ = "4"
t = timer for i = 0 to 1000000 if asc(s$) >= asc("0") and asc(s$) <= asc("9") then end if next t = timer - t print "time elapses[1]: ";t
t = timer min_char = asc("0") max_char = asc("9") s_asc = asc(s$) for i = 0 to 1000000 if s_asc >= min_char and s_asc <= max_char then end if next t = timer - t print "time elapses[2]: ";t
|
|
|
Post by eddavis2 on Sept 19, 2022 18:53:30 GMT -6
I think the way you did it here is the best way to do it in RCBasic currently. You could do some optimization on your code though: Run this and look at the difference. Its 3 to 4 times faster to just store your min and max character. s$ = "4"
...
Cool! Thanks!
|
|
|
Post by aurel on Sept 20, 2022 6:47:37 GMT -6
I never tested before this functions in RCB but looks that INSTR( string, substring) return a wrong result in Ed case should return 0 because substring not exists in main string instead of that i get -1 ....TRUE then i try this : p = instr("abcd", "c") print p and i get 2 ..which is also wrong should be 3...right ?
|
|
|
Post by johnno56 on Sept 20, 2022 6:57:55 GMT -6
Same result of 2 on Linux via Geany... Riddle me that one, Batman...
|
|
|
Post by n00b on Sept 20, 2022 8:34:33 GMT -6
aurel johnno56 The first character in a string is at index 0. Those results are correct.
|
|
|
Post by aurel on Sept 20, 2022 11:35:00 GMT -6
well i am not sure why is 0 ,,,is your string a array of char type ?
i don't have yet implemented INSTR() function in microA but in o2 it is 3 look screenshot
|
|
|
Post by aurel on Sept 20, 2022 11:43:27 GMT -6
Then look at this CreativeBasic instr() which array by the way start with index 0 also result is 3 ...
|
|
|
Post by johnno56 on Sept 20, 2022 13:50:44 GMT -6
Index 0.. I should have remembered that from the "Mastermind" (bulls and cows).... Just tested using 1985 Locomotive Basic, you guessed it, 3...
Quick question. Has the first index of arrays always been zero? This is getting to be a similar situation with "rnd"... I am curious as to why there is not a "standard" for all this stuff. Makes conversions a bit confusing... lol
Zero or one... I can deal with both... but should I? Moo Ha Ha...
J
|
|
|
Post by aurel on Sept 20, 2022 23:47:35 GMT -6
0 or 1 ..something is wrong with instr() or NOT... if not existing substring give result -1 then that -1 mean ...not existt
|
|
|
Post by n00b on Sept 21, 2022 21:01:01 GMT -6
0 or 1 ..something is wrong with instr() or NOT... if not existing substring give result -1 then that -1 mean ...not existt Its supposed to return -1 if the search string is not found. Returning 0 would not make sense when 0 is the first character in the string. This is also how most programming languages work in general. And RCBasic has always followed C/C++ indexing on strings and return values regarding strings. If you look in the manual for InStr(), it does mention that it returns -1 on failure.
|
|