Recursion Limit
#1
Ewh! disappointing that QB64pe dies in mid 18,000's BaCon and FreeBasic do way better...

https://rosettacode.org/wiki/Find_limit_...sion#BASIC

Here is code I checked with, maybe there is better?
Code: (Select All)
howRecursive 1
Sub howRecursive (i As _Integer64)
    If i < 0 Then Print "_Integer64 turned negative.": End
    Print i
    _Delay .000000000005
    howRecursive i + 1
End Sub

Maybe with manual stack?
b = b + ...
Reply
#2
BAM (well, javascript) chokes at just a little over 526,200.  

BAM version of the test program:

Code: (Select All)
Sub howRecursive (i As long)
    If i < 0 Then Print "_Integer64 turned negative.": End
    if i mod 300 = 0 then Print i: _display
    howRecursive(i + 1)
End Sub

howRecursive(1)

I wanted to see how QBJS handles my BAM program (I was expecting same results), but QBJS is giving the error:

Code: (Select All)
WARN : 4 : Missing or unsupported method: 'howRecursive(i' - ignoring line
ERROR : 0 :
Unexpected token '}'
SyntaxError: Unexpected token '}'
    at new AsyncFunction ()
    at runProgram (https://qbjs.org/qbjs-ide.js:234:22)
Reply
#3
How interesting is this.  Adding additional code in the subroutine causes the BAM program to choke earlier.

It kind of makes sense if it is about some memory limit getting reached.  Maybe that's what is going on with QB64pe ???

The following version of the program chokes at just a little over 420, 900:

Code: (Select All)
Sub howRecursive (i As long)
    If i < 0 Then Print "_Integer64 turned negative.": End
    if i mod 300 = 0 then Print i: _display
    if i > 520000 then print "press a key to continue" : _display : temp$ = input$(1)
    howRecursive(i + 1)
End Sub

howRecursive(1)
Reply
#4
(05-28-2023, 02:04 PM)bplus Wrote: Here is code I checked with, maybe there is better?

Without _DELAY and outputting to console:

[Image: howrecursive-seg-fault.png]
Reply
#5
(05-28-2023, 02:39 PM)CharlieJV Wrote: I wanted to see how QBJS handles my BAM program (I was expecting same results), but QBJS is giving the error:

Code: (Select All)
WARN : 4 : Missing or unsupported method: 'howRecursive(i' - ignoring line
ERROR : 0 :
Unexpected token '}'
SyntaxError: Unexpected token '}'
    at new AsyncFunction ()
    at runProgram (https://qbjs.org/qbjs-ide.js:234:22)

@bplus' original example runs without modification just fine in QBJS.  There were a couple of issues keeping your BAM version from running in QBJS:
1) You are calling a sub with parameters, but without a CALL statement.  You need to either remove the parameters or prefix the call to the sub with the CALL statement.
2) Main code must be defined before subs and functions

This modified version runs fine:
Code: (Select All)
howRecursive 1

Sub howRecursive (i As long)
    If i < 0 Then Print "_Integer64 turned negative.": End
    if i mod 300 = 0 then Print i: _Delay .000000000005
    'if i > 520000 then print "press a key to continue" : sleep
    howRecursive i + 1
End Sub

I would expect some variation between browsers and system specs.  I'm running the code in my Edge browser.  It's still running now and just passed 13 million:

   
Reply
#6
Oh you guys and gals have been busy!

I was testing other Basics and learned LB could go to 4 million +

Well I doubled that in QB64pe v3.7 with same code tip: use a GoSub!

Code: (Select All)
Dim i As _Integer64
i = 1
GoSub howRecursive
End
howRecursive:
If i < 0 Then Print "_Integer64 turned negative.": End
Print i
_Delay .000000000005
i = i + 1
GoSub howRecursive
Return

I was up past 8 million or was it 81 million? I stopped program to get my computer back to do more stuff.

Oh hey! Works way way way faster w/o delay (the key word = works) Smile 
Way past 200 million as I write this.

3 Billion+ whats to stop this? The _Integer64 Type I think.

Oh got over .5 Billion not 3 Billion, misread numbers:
   
b = b + ...
Reply
#7
@bplus
in the QB64pe IDE click on Options->Compiler Settings and in the C++ Linker Flags: put -Wl,--stack,134217728
that's 128MB or 2^27
here's my mod to your code
Code: (Select All)
$NoPrefix
$Console:Only
Dest Console
howRecursive 1
Sub howRecursive (i As _Integer64)
    If i < 0 Then Print "_Integer64 turned negative.": End
    Print i
    ' _Delay .000000000005
    howRecursive i + 1
End Sub

compile to exe and then launch the cmd and from the cmd run your program, if your program dies you will still have the printout in the cmd window

here's my result for different linker values
2m 14378
4m 28943
8m 58076
16m 116325
32m 232839
64m 465856
128m 931886
Reply
#8
(05-28-2023, 06:10 PM)bplus Wrote: Oh you guys and gals have been busy!

I was testing other Basics and learned LB could go to 4 million +

Well I doubled that in QB64pe v3.7 with same code tip: use a GoSub!

Code: (Select All)
Dim i As _Integer64
i = 1
GoSub howRecursive
End
howRecursive:
If i < 0 Then Print "_Integer64 turned negative.": End
Print i
_Delay .000000000005
i = i + 1
GoSub howRecursive
Return

I was up past 8 million or was it 81 million? I stopped program to get my computer back to do more stuff.

Oh hey! Works way way way faster w/o delay (the key word = works) Smile 
Way past 200 million as I write this.

3 Billion+ whats to stop this? The _Integer64 Type I think.

Oh got over .5 Billion not 3 Billion, misread numbers:

For the giggles, I decided to do the same sort of thing with BAM, and it conked out at 536.85 million.  That's a big difference from before.

I'm finding it wildly interesting that both BAM's BASIC interpreter written in javascript and QB64pe's compiled C++ perform both way better using GOSUB instead of Sub.

Suddenly, I'm starting to question my attitude towards GOSUB.


Code: (Select All)
dim as double i
i = 1

GOSUB howRecursive

end

howRecursive:
    If i < 0 Then Print "_Integer64 turned negative.": End
    if i mod 50000 = 0 then Print i: _display
    i = i + 1
    GOSUB howRecursive
return
Reply
#9
Quote:if your program dies you will still have the printout in the cmd window

Clever @Jack, do you still need delay? I didn't when used the GoSub version.


Quote:with BAM, and it conked out at 536.85 million.
@CharlieJV Looks like we get similar results with GoSub and yeah, it still has some surprises for us Smile
b = b + ...
Reply
#10
@dbox now I see why you wrote the version you did, this is about half an hour in (still running):
   

But without some delay it wont work at all, or just seems to sit there no error, no printing.
Without the delay QB64pe just does a wait circle and then crashes.
b = b + ...
Reply




Users browsing this thread: 2 Guest(s)