1

I'm very new to Groovy. I'm solving a Fibonacci series with its properties. I have a groovy code like this :

a = new BigInteger[2][2]
a[0][0] = 1
a[0][1] = 1
a[1][0] = 1
a[1][1] = 0
temp = new BigInteger[2][2]
temp = a
def testFun(def a,BigInteger n) {
    if(n == 1)
        return a
    b = new BigInteger[2][2]
    def sum = 0
    for ( c = 0 ; c < 2 ; c++) {
                for ( d = 0 ; d < 2 ; d++ )
                {   
                   for ( k = 0 ; k < 2 ; k++ )
                   {
                      sum = sum + temp[c][k] * a[k][d];
                   }
           b[c][d] = sum
               sum = 0;
            }
         }
        testFun(b,n-1)
}
c = new BigInteger[2][2]
BigInteger n = 90
if( n % 2 == 0 )
{   
        c =  testFun(a,n.divide(2))
        temp = c
        c = testFun(c,2)
}
println c[0][1]

I'm getting answer like :

2880067194370816120

But when I change n value to say 5090 , I get a big error like this :

at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)
    at fibb.testFun(fibb.groovy:24)
    at fibb$testFun.callCurrent(Unknown Source)

What went wrong? Why I'm getting this error?

batman
  • 4,728
  • 8
  • 39
  • 45

1 Answers1

2

Although I would have expected to see a StackOverflowException in that stacktrace, perhaps you just snipped it out. I think you're running into the maximum recursion depth, which is why it works for a small value of n but fails with a large value. You want to look into closure trampolining, part of Groovy 1.8. Here's the relevant section of the release notes.

There is also this question which is similar and should help you out.

By the way, the reason it says "Unknown Source" in the stack trace is because Groovy doesn't have its source available to tell you which line it was on - it's a red herring.

Community
  • 1
  • 1
doelleri
  • 19,232
  • 5
  • 61
  • 65
  • Would tail recursion optimization prevent a stack overflow, or is that not what it does? – Waleed Khan Jul 18 '12 at 17:50
  • 1
    +1 it's probably being run in the Groovy console which has problems displaying massive stacktraces such as you get with stack overflow exceptions – tim_yates Jul 18 '12 at 17:52
  • [@arxanas](http://stackoverflow.com/questions/11546969/what-does-this-unknown-source-error-mean#comment15270006_11547345) Groovy doesn't do tail call elimination. It does have [trampolines](http://java.dzone.com/articles/cool-stuff-groovy-18) though. – Justin Piper Jul 18 '12 at 19:01
  • I'm trying tail recursion optimization in my code, Groovy doesn't support it? – batman Jul 19 '12 at 02:40
  • This [blog](http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html) I think explains the situation fairly well. The JVM does not currently support tail-call optimization, although it is possible Groovy could do some limited optimizations. See also this [question](http://stackoverflow.com/questions/3616483/why-does-the-jvm-still-not-support-tail-call-optimization) – doelleri Jul 19 '12 at 03:19