Author Topic: BASIC difference  (Read 1138 times)

B+

  • Full Member
  • ***
  • Posts: 214
    • View Profile
BASIC difference
« on: April 20, 2015 »
Here are some notes to those with some Basic experience:
Code: [Select]
rem string test.txt 2015-04-19 B+
rem what the heck happened to right$ function the other night?
rem Maybe I tried Right$ not right$
rem Maybe Naalaa counts between letters like Python does
test$="Hi NaaLaa"
wln "test$="+test$+", with length of:"+str$(len(test$))
wln
for i=0 to len(test$)+1
write "For i="+str$(i)+":"
write "  left$(test$,i)="+left$(test$,i)
write "  mid$(test$,i)="+mid$(test$,i)
write "  right$(test$,i)="+right$(test$,i)
wln
wln
next
wln "results: mid$ is one off normal basic experience, 0 to len(word$)-1"
wln "right$ is wacky as all heck, no wonder I had such a time the other night!"
wln "left$ is the only normal, BTW mid$ does 1 charater automatic with 2 parameter."
wln "What is nice is that it doesn't error out when i exceeds the string length!"
wait keydown
B+

B+

  • Full Member
  • ***
  • Posts: 214
    • View Profile
Re: BASIC difference
« Reply #1 on: April 30, 2015 »
Some impressive comparisons to SmallBasic after Simple Sieve modified in a couple of places, ironically I discover them while playing with the slowest BASIC I've ever encountered, BASIC 256:

Code: [Select]
rem Simple Sieve v2.txt 2015-04-30 B+ some improvements
listmode=1
d=1000
rem Simple Sieve before V 2
rem   1,000,000     78,498 primes in     967 msecs,     961 msecs
rem  10,000,000    664,579 primes in  10,100 msecs,  10,123 msecs
rem 100,000,000  5,761,455 primes in 109,596 msecs, 109,872 msecs
rem V 2 changes
rem   1,000,000     78,498 primes in     931 msecs,     914 msecs
rem  10,000,000    664,579 primes in   9,940 msecs,   9,834 msecs
rem 100,000,000  5,761,455 primes in 106,775 msecs, 106,976 msecs
set window 0,0,800,600
toc=time()

wln "Primes to "+str$(d)+":"
wln
c[d+1]
cnt=0
i=4
while i<=d
c[i]=1
i=i+2
wend
cnt=1
if listmode=1 then write rite$("     "+str$(2),5)
i=3
while i<=d
if c[i]=0
cnt=cnt+1
if listmode=1
write rite$("    "+str$(i),5)
if cnt % 15=0 then wln
endif
if i*i<=d
j=2*i
          while j<=d
                   c[j]=1
               j=j+i
            wend
endif
endif
i=i+2
wend
tic=time()
wln
wln
wln "prime count is "+str$(cnt)+", in "+str$(tic-toc)+" msecs."
rem better be 168 for 1000
wait keydown
end
function rite$(stringy$,amount)
l=len(stringy$)
return mid$(stringy$,l-amount,amount)
endfunc

SmallBASIC couldn't get past 25,000,000 and was doing 10,000,000 in over 20 secs.
But with that I could STEP through FOR/NEXT loops...

B+
B+

Hemulius

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: BASIC difference
« Reply #2 on: August 27, 2015 »
Translated Simple Sieve to VM, for fun and exercise mainly.
Code: [Select]
rem Simple Sieve v2.txt 2015-04-30 B+ some improvements
rem listmode=0 ...related lines removed for convenience
d=1000000

set window 0,0,800,600
toc=time()

wln "Primes to "+str$(d)+":"
wln
rem ---------

c[d+1]
cnt = 0
i = 4
MOV @0 c
@start:
STP @0 i
MOV [@0] 1
ADD i 2
CMP d 1
JLE start
cnt = 1
i = 3
j = 0

@inner:
rem while i<=d
rem if c[i]=0
MOV @0 c
STP @0 i
CMP [@0] 0
JNE exit

rem cnt=cnt+1
        ADD cnt 1

rem if i*i<=d
MOV @0 i
MUL @0 i
CMP @0 d
JG exit

rem j=2*i
MOV @0 i
MUL @0 2
MOV j @0

rem while j<=d
MOV @0 j
CMP @0 d
JG exit

@outer:
rem c[j]=1
MOV @0 c
STP @0 j
MOV [@0] 1

rem j=j+i
        ADD j i

MOV @0 j
CMP @0 d
JLE outer

@exit:
rem i=i+2
        ADD i 2

MOV @0 i
CMP @0 d
JLE inner

rem ---------
tic=time()
wln
wln "prime count is "+str$(cnt)+", in "+str$(tic-toc)+" msecs."
wait keydown
end

« Last Edit: August 31, 2015 by Hemulius »

aurel

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
Re: BASIC difference
« Reply #3 on: September 03, 2015 »
Ha Ha ha...
anyone of you master jumper can explain this experssion:

set prime = i + i + 3

Rick3137

  • Sr. Member
  • ****
  • Posts: 319
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: BASIC difference
« Reply #4 on: September 05, 2015 »
Looks like Greek to me.

B+

  • Full Member
  • ***
  • Posts: 214
    • View Profile
Son of B!
« Reply #5 on: August 30, 2016 »
  :o  >:(  :(
Code: [Select]
' compare strings test.txt for Naalaa [B+=MGA] 2016-08-30
' I spent a couple of frustrating hours before realizing...

set window 100, 40, 800, 600
a$ = "1"

'alternate next line 0, 1, 2
b$ = "0"

if a$ = b$ then
wln a$ + " = " + b$
elseif a$ < b$ then
wln a$ + " < " + b$
elseif a$ > b$ then
wln a$ + " > " + b$

'oh add this test! Yes can detect a difference!
elseif a$ <> b$ then
wln a$ + " <> " + b$

else
wln "Naalaa does not compare strings at all!"
endif

'another test, will this erase
set caret 0, 0
wln "                        "
'result NO! could have been useful

set caret 0, 0
wln "______"

wait keydown
B+

Marcus

  • Administrator
  • Sr. Member
  • *****
  • Posts: 497
    • View Profile
Re: BASIC difference
« Reply #6 on: August 31, 2016 »
Nah, naalaa just uses < and > to compare the lengths of strings for some reason. Might just be a leftover since the early days of naalaa (no, i don't mess around with strings much). I'll change it to a character based comparison in the next update, don't think anyone has ever used < or > anyway.
.\\\opz

Marcus

  • Administrator
  • Sr. Member
  • *****
  • Posts: 497
    • View Profile
Re: BASIC difference
« Reply #7 on: August 31, 2016 »
To be sure, what you're looking for is this:

Code: [Select]
function StringGreater(a$, b$)
if strcmp(a, b) > 0 then return true
return false
endfunc

function StringGreaterOrEqual(a$, b$)
if strcmp(a, b) >= 0 then return true
return false
endfunc

function StringSmaller(a$, b$)
if strcmp(a, b) < 0 then return true
return false
endfunc

function StringSmallerOrEqual(a$, b$)
if strcmp(a, b) <= 0 then return true
return false
endfunc

function strcmp(a$, b$)
la = len(a); lb = len(b)
for i = 0 to min(la, lb) - 1
ca = asc(mid(a, i)); cb = asc(mid(b, i))
if ca > cb; return 1
elseif ca < cb; return -1
endif
next
if la > lb; return 1
elseif la < lb; return -1
endif
return 0
endfunc

but applied to >, >=, < and <=?

Sorry for the inconvenience, I admit not using strings much at all for anything :)
.\\\opz

B+

  • Full Member
  • ***
  • Posts: 214
    • View Profile
Re: BASIC difference
« Reply #8 on: August 31, 2016 »
To be sure, what you're looking for is this:

Code: [Select]
function StringGreater(a$, b$)
if strcmp(a, b) > 0 then return true
return false
endfunc

function StringGreaterOrEqual(a$, b$)
if strcmp(a, b) >= 0 then return true
return false
endfunc

function StringSmaller(a$, b$)
if strcmp(a, b) < 0 then return true
return false
endfunc

function StringSmallerOrEqual(a$, b$)
if strcmp(a, b) <= 0 then return true
return false
endfunc

function strcmp(a$, b$)
la = len(a); lb = len(b)
for i = 0 to min(la, lb) - 1
ca = asc(mid(a, i)); cb = asc(mid(b, i))
if ca > cb; return 1
elseif ca < cb; return -1
endif
next
if la > lb; return 1
elseif la < lb; return -1
endif
return 0
endfunc

but applied to >, >=, < and <=?

Sorry for the inconvenience, I admit not using strings much at all for anything :)

I worked out similar but yours is more elegant  :)  much more!!

BTW, I like how MID does not error when asking for neg position or one greater than length of source string.
B+

Marcus

  • Administrator
  • Sr. Member
  • *****
  • Posts: 497
    • View Profile
Re: BASIC difference
« Reply #9 on: August 31, 2016 »
BTW, I like how MID does not error when asking for neg position or one greater than length of source string.

Haha! Sorry, I can add a runtime error for that.
.\\\opz