Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack overflow in Fibonacci tail recursion #28

Open
mabDc opened this issue Apr 15, 2021 · 6 comments
Open

Stack overflow in Fibonacci tail recursion #28

mabDc opened this issue Apr 15, 2021 · 6 comments
Labels
bug Something isn't working compiler

Comments

@mabDc
Copy link

mabDc commented Apr 15, 2021

Stack Overflow when calc Fibonacci tail recursion

fun fib(n, a, b){
    return n == 1 ? a : fib(n-1, a + b, a)
}
fun main(){
    print(fib(1234, 1, 1))
}
@hythl0day
Copy link
Member

hythl0day commented Apr 15, 2021

I tried my self with this version:

      fun fib(n0, n1, c){
        if (c == 0) {
          return n0
        }
        else if (c == 1) {
          return n1
        }
        return fib(n1, n0 + n1, c - 1)
      }

      fun main(){
          print(fib(0, 1, 1020))
      }

I can go as far as 1020, on 1021, it returned a negative number I don't know why ,and beyond that is stack overflow...

It might be because the bytecode version interpreter right now hetu use doesn't support BigInt like Dart does.

Or it is because there are some other limit this recursion touched...

@hythl0day
Copy link
Member

And the result is wrong when the nth is higher, I'm still testing.

@hythl0day
Copy link
Member

hythl0day commented Apr 15, 2021

after fib(92), the result reached the limit of int64(which is the limit of the current bytecode interpreter).

however why the stack overflowed after 1020 is still unclear

@rockingdice
Copy link
Contributor

It's quite near 1024.
Maybe it's a limitation set by the OS or Dart VM.

@mabDc
Copy link
Author

mabDc commented Apr 15, 2021

@rockingdice let's be close to the topic: tail recursion should not cause stack overflow that make sense

@rockingdice
Copy link
Contributor

@mabDc Sure, so this could be a bug.

@rockingdice rockingdice added the bug Something isn't working label Apr 15, 2021
@hythl0day hythl0day changed the title stack overflow Stack overflow in Fibonacci tail recursion Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler
Projects
None yet
Development

No branches or pull requests

3 participants